Hi I'm a python dev (among other things), trying to learn Django and I'm already stuck at the hello world. It's tricky cause I'm not quite sure what I'm dealing with at this stage, so maybe you can help me figure it out.
I've got the environment configured via anaconda 2.x, I've tested everything was working until I created the first app. So here are the sources, maybe you can tell what's causing the error.
File structure
For the project:
settings.py
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'pages_app.apps.PagesAppConfig',
]
urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('pages_app/', include('pages_app.urls')),
]
For the pages_app:
apps.py
from django.apps import AppConfig
class PagesAppConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'pages_app'
urls.py
from django.urls import path
from . import views
url_patterns = [
path('', views.homePageView, name='home'),
]
views.py
from django.http import HttpResponse
def homePageView(request):
return HttpResponse("Hello, world. You're at the polls index.")
I'll note that this is my second attempt at hello world app, first one was deleted after I found out name "pages" was taken by a python package in my general_env and I deleted it, all references and created pages_app with the manage.py startapp pages_app command. Before I recreated this renamed app there was already an error, just different.I'm hoping it has nothing to do with other packages otherwise it will be a pain.
I'll be grateful if anyone can help me figure out the problem. Thanks!
Edit: I've tried setting up a new environment and installing just django, but the problem persists, so it doesn't appear to be a conflict with package names. I've also seen two versions of adding apps urls to project urls
path('', include('pages_app.urls'))
path('pages_app/', include('pages_app.urls')),
in both cases I got the same error.
CodePudding user response:
The problem is the url_patterns
variable you defined. Try changing it to urlpatterns
instead of url_patterns
under the urls.py of your pages_app.
Django loads that Python module and looks for the variable urlpatterns. This should be a sequence of django.urls.path() and/or django.urls.re_path() instances.
CodePudding user response:
I'm no expert in Django, but from the console log I'd say that something may be wrong with the path() function.
CodePudding user response:
After continuous testing different configurations and googling I came by this
Trying to trace a circular import error in Django
and I've made the same mistake. I mistyped urlpatterns
, weirdly enough I was sure that's how the tutorial had it and upon looking it up again, I noticed it's written without the _
in between.
So there you have it. Sorry for the fuss, I hope it helps someone.