I am trying to add a django-oscar shop to an existing django website.
My problem is that the templates of the two are somehow clashing, such that I can either see the existing website, or the shop, but not both.
Here is the overarching urls.py:
from django.conf.urls import include, url
from django.contrib import admin
from oscar.app import application
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^', include('main.urls')),
# oscar
url(r'^shop/', include(application.urls)),
]
And in the settings:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
# os.path.join(BASE_DIR, 'templates'),
OSCAR_MAIN_TEMPLATE_DIR
],
# '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',
'oscar.apps.search.context_processors.search_form',
'oscar.apps.promotions.context_processors.promotions',
'oscar.apps.checkout.context_processors.checkout',
'oscar.apps.customer.notifications.context_processors.notifications',
'oscar.core.context_processors.metadata',
'main.context_processors.google_analytics'
],
'loaders': [
'django.template.loaders.app_directories.Loader',
'django.template.loaders.filesystem.Loader',
],
},
},
]
If I switch the order of the loaders, either the original website (in app 'main'), or the Oscar shop, can no longer be accessed/viewed. So I'm not sure what esoteric detail I'm overlooking, and the docs don't cover this. Cheers.
The issue you're having is that your template names conflict with Oscar's. Oscar has it's own base.html, which is what the template loader will find if you list that loader first, instead of your own base.html. Django will use the first one it finds.
This is a known issue with Oscar - unfortunately there is no backwards compatible way to fix it, and so it hasn't been addressed for some time.
Changing Oscar's behaviour is quite difficult so I'd suggest you try and change your template structure instead. Specifically, you should namespace all your app templates. So if your app is called myapp, then put the base template in myapp/templates/myapp/base.html. You would then refer to this in other templates as {% extends 'myapp/base.html' %}. Similarly put all other templates in templates/myapp/.
This will ensure that your templates do not clash with Oscar's, and the problem should go away.
Related
I'm new to Django and I want to use my custom login page instead of the ugly looking django login template. How could I make it work? I think I need to edit the urls.py file and override the login template but I don't know how to do it. Thanks
urls.py
from django.urls import path
from django.contrib.auth.views import (
LoginView,
)
urlpatterns = [
path('login/', LoginView.as_view(template_name='login/login.html'), name='login'),
]
settings.py
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'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',
],
},
},
]
LOGIN_URL = '/login/'
Now make a templates folder in the Base directory( where your manage.py is present) and in that folder make a new folder named login. Inside login folder make login.html
So the directory structure will be like this
-my_project
-templates
-login
-login.html
-manage.py
Now do whatever customization you want to do in login.html. You will get form object in context of the template, which is nothing but your login form.
Django version 2.2.6
I have the following folder structure and was expecting Django to find index.html based as it is placed in the default location under templates.
Am I doing something wrong here? Did Django stop looking up the template paths by default?
-app
|
settings.py
urls.py
...
templates
|
base.html
index.html
views.py
from django.shortcuts import render
# Create your views here.
def home_view(request):
return render(request, 'index.html')
urls.py
from django.contrib import admin
from django.urls import path
from .views import home_view
urlpatterns = [
path('', home_view, name='index'),
path('admin/', admin.site.urls),
]
You need to set the path of your templates folder in settings.py.
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.request',
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
Leave DIRS blank and try again.
you are importing the home view page wrongly in the urls.py
I am using the default authentication system of Django (v1.8.12), but I want to create my own templates (actually according to the documentation django.contrib.auth do not provide any template).
Then I added the folder 'registration' inside the 'templates' folder of my application 'mainApp'. According to the documentation this is the right place, a folder named 'registration'.
I have also created the template 'login.html', and this template is loaded properly at the defined url '...account/login'.
The problem is with the other templates:
logged_out.html
password_change_done.html
password_change_form.html
password_reset_complete.html
password_reset_confirm.html
password_reset_done.html
password_reset_email.html
password_reset_form.html
The previous templates are been loaded from the 'admin' application and not from my app.
How can I tell Django that my 'registration' templates should be loaded and not the templates defined in the admin application?
My project url.py is
urlpatterns = [
url(r'^', include('mainApp.urls', namespace="mainApp")),
url(r'^admin/', include(admin.site.urls)),
url(r'^account/', include('django.contrib.auth.urls')),
]
My template settings are the default settings
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'debug': True,
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
Just move mainApp above django.contrib.admin in your INSTALLED_APPS setting, so that Django searches the mainApp/templates directory first.
That way, you don't need to add mainApp/templates to your DIRS setting.
Very simple question. When using the login_required decorator above a view, the authentication system redirects me to whatever URL is defined in LOGIN_URL in settings.py, using the template found in registration/login.html.
The question: How can I define a different template name for my login form (I don't want to use the default)?
login documentation:
If you’d prefer not to call the template registration/login.html, you can pass the template_name parameter via the extra arguments to the view in your URLconf. For example, this URLconf line would use myapp/login.html instead:
(r'^accounts/login/$',
'django.contrib.auth.views.login',
{'template_name': 'myapp/login.html'}),
----Django 1.10--------
I changed TEMPLATE_DIR in settings to solve this problem
setting.py
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR,'templates')],
'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',
],
},
},]
urls.py
url(r'^login/$', auth_views.login, {'template_name': 'registration/login.html'}, name='login'),
and place the registration file directory under templates directory!
I'm attempting to get the Django development server to load a template with the following settings but it's throwing the error
TemplateDoesNotExist at /
homepage/index.html
Below are the files that I've edited to try to get this to work
blog/blog/settings.py - (Cut the portion pertaining to this question)
import os
TEMPLATE_DIRS = (
os.path.join( os.path.dirname(__file__), 'templates' ),
)
blog/apps/homepage/views.py
from django.shortcuts import render_to_response
def index(request):
return render_to_response('homepage/index.html')
Any thoughts about how to fix this?
By default Django uses two template loaders to locate templates:
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
)
First the filesystem loader checks all dirs in TEMPLATE_DIRS to see if it can find the named template. If that fails the app_directories loader will look in the /templates/ dir in the current app for the named template. This way you can ship default templates with reusable apps that can easily be overridden.
In your case Django will look for the template here:
blog/blog/templates/homepage/index.html
blog/apps/homepage/templates/homepage/index.html
You give a wrong direction to your TEMPLATE_DIRS. The settings.py file is in package blog in project blog, but your templates folder is in blog project, not in blog package. You must give the absolute directory of your project and join templates with it.
in django 1.10, navigate to path/to/settings.py in your parent project directory, look for:
TEMPLATES = [.....] and specify the path/to/project_directory in the DIR: option
for example (in my own case):
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
'/xampp/htdocs/advisory_portal'
],
'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',
],
},
},
]