Reputation: 455
I'm using Django 1.9.8 and started learning by following the official tutorial. The official tutorial emphasised reusabilitiy and "plugability". From there I followed this tutorial on authorization. Although I was able to get the authorization tutorial to work, one thing about it that I didn't like (or just don't understand) is why the project's urls.py
file contains several app specific urls, rather than placing them in the app's urls.py
file and just including that file in the project's urls.py
file. That seems to go against what the official tutorial emphasizes. I understand each project may have different URL's for login/logout/register, etc... depending on the API and will still have to be edited, but I feel like changing them in one place makes more sense and keeps things neater.
The name of the project is authtest and the name of the app is log
#log/urls.py
from django.conf.urls import url, include
from django.contrib import admin
from django.contrib.auth import views
from log.forms import LoginForm
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'', include('log.urls')),
url(r'^login/$', views.login, {'template_name': 'login.html', 'authentication_form': LoginForm }), #move this to authtest/urls.py
url(r'^logout/$', views.logout, {'next_page': '/login'}), #move this to authtest/urls.py
]
Now for the app's urls.py
file
#authtest/urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.home, name='home'),
]
This works 100%, so now for the first question. Is there any reason I shouldn't move the log app specific urls (login & logout) out of the project's urls.py file (log/urls.py) and put them into the app's urls.py file (authtest/urls.py)? Maybe there are reasons for authentication not to, but what about if I was making a different app?
Now for my second question, which I suppose depends on the answer to the first question. The authorization tutorial places the login.html, logout.html, and home.html templates in the project's root templates folder. The Django tutorial suggests putting them within an app's templates directory, and within that directory, another directory named whatever the app is called (for namespacing). What do I have to change if I move the app specific template files from the project's templates folder, to the log app's templates folder?
This is the current file structure from the authorization tutorial I followed
authtest
|...authtest
|...|...settings.py
|...|...urls.py
|...log
|...|...settings.py
|...|...urls.py
|...|...views.py
|...manage.py
|...templates
|...|...base.html
|...|...home.html
|...|...login.html
|...static
This is how I assumed it should be based on how the official tutorial suggests to use templates.
authtest
|...authtest
|...|...settings.py
|...|...urls.py
|...log
|...|...urls.py
|...|...views.py
|...|...templates
|...|...|...log #namespace of the log app
|...|...|...|...base.html
|...|...|...|...home.html
|...|...|...|...login.html
|...manage.py
|...templates
|...static
When I move the files I get the following error TemplateDoesNotExist at /login/
when I visit http://localhost:8080/login/
. I'm assuming it's solely the urls.py files, but I'm not sure exactly what I have to change.
edited for settings.py templates directive
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ["templates"],
#'DIRS': [os.path.join(BASE_DIR, 'templates')], #I also tried this
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
Upvotes: 0
Views: 2593
Reputation: 4816
In your settings.py
you need to add the loaders
key in OPTIONS
section. This specifies how django finds your template files
. If you weren't specifying the OPTIONS
key, the APP_DIRS
settings should have been enough.
TEMPLATES = [
{
# See: https://docs.djangoproject.com/en/dev/ref/settings/#std:setting-TEMPLATES-BACKEND
'BACKEND': 'django.template.backends.django.DjangoTemplates',
# See: https://docs.djangoproject.com/en/dev/ref/settings/#template-dirs
'DIRS': [
str(APPS_DIR.path('templates')),
],
'OPTIONS': {
# See: https://docs.djangoproject.com/en/dev/ref/settings/#template-debug
'debug': DEBUG,
# See: https://docs.djangoproject.com/en/dev/ref/settings/#template-loaders
# https://docs.djangoproject.com/en/dev/ref/templates/api/#loader-types
'loaders': [
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
],
# See: https://docs.djangoproject.com/en/dev/ref/settings/#template-context-processors
},
},
]
https://docs.djangoproject.com/en/1.10/ref/templates/api/#loader-types for more info
Upvotes: 2