I have been trying to install django-oscar but it gives me this error when I try to make migrations (via : python manage.py migrate).
ImportError: cannot import name 'OSCAR_MAIN_TEMPLATE_DIR' from 'oscar'
(C:\Users\dell\Envs\eshop\lib\site-packages\oscar__init__.py)
Code I am using:
from oscar import OSCAR_MAIN_TEMPLATE_DIR
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.template.context_processors.i18n',
'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',
],
},
},
]
From the release notes for 2.0+ (2019-07-04):
OSCAR_MAIN_TEMPLATE_DIR setting has been removed and existing templates updated with the full path. See issue:1378, issue:2250. Please update your templates accordingly.
Related
I'm following along a project from the Python crash course book. I'm supposed to be deploying the webapp to heroku but I keep receiving a Server Error 505 warning.
The problem:
I have tried to do various things with my setings.py file, but none appear to work.
SETTINGS.PY:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [r'\learning_logs\templates\learning_logs',
r'\users\templates\users',],
'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',
],
},
}, ]
Project setup:
learning_log: 11_env learning_log:
.git
learning_log:
....
learning_logs:
templates:
learning_logs:
*all html files including base.html*
...
If anyone requires more information to answer the question, please let
me know.
I am trying to connect Flutter with Django. Flutter and Django alone seems to be fine,working without error. But when i am trying to combine both together, an error pops up that says:
TemplateDoesNotExist at /accounts/
Here is the cause of the problem
from django.shortcuts import render, HttpResponse
def home(request):
return render(request, '../screens/login_screen.dart')
It says that directory does not exists.
As you see above the directory exists. What is the problem can somebody help?
Template:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'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',
],
},
},
]
What I can see is that the screens folder is on the root but not inside the accounts folder it's self.
For that you have to explicitly mention in the templates configurations within settings. You have to mention the "BASE_DIR, 'name of the folder where the templates are'". Here how it has to be written.
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR,'screens')],
'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',
],
},
},
]
Secondly, you have to give the path of the file within in the views in such manner.
return render(request, 'screens/login_screen.dart', context)
Hope that helps.
Is there a way in django to not need the {% load static %} at the top of every template?
This question indicates you can factor out common load tags into settings, but doesn't give the particulars you need in this case.
As of Django 1.9, you can add a builtins key to your TEMPLATES["OPTIONS"] in settings.py.
For Django 2.1+, use:
'builtins': ['django.templatetags.static']
For Django 1.9 - 2.0 (this will work up until 2.2, after which it is deprecated), use:
'builtins': ['django.contrib.staticfiles.templatetags.staticfiles']
For example, the whole template setting might look like this:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'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',
],
'builtins': ['django.templatetags.static'],
},
},
]
Thanks to #ZachPlachue for the Django 3 update.
The previous answer's method is deprecated as of Django 3.0. (see : https://docs.djangoproject.com/en/3.0/releases/3.0/#features-removed-in-3-0)
Now you'd need to add the following to your template settings:
'builtins': ['django.templatetags.static']
This is the updated templates setting:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'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',
],
'builtins': [
'django.templatetags.static',
],
},
},
]
I'm trying to use unipath module in my django project as it mentioned in Two Scoops of Django book and something works not as intended.
In my settings.py I have this:
from unipath import Path
BASE_DIR = Path(__file__).ancestor(2)
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [(BASE_DIR.child('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',
],
},
},
]
STATIC_ROOT = BASE_DIR.child('static')
MEDIA_ROOT = BASE_DIR.child('media')
And with that settings I'm getting TemplateDoesNotExist exception:
Using loader django.template.loaders.filesystem.Loader:
/var/www/(Path('/home/user/my_project/project/templates'),)/myapp/index.html (File does not exist)
What am I doing wrong?
Finally solved this problem!
Need to edit TEMPLATES' 'DIRS' string in settings.py to:
'DIRS': (BASE_DIR.child('templates'),),
I wrote an app in django. And I want to write a index.html for it. But I can't make it run. I tried and inspect the settings and code but, it doesn't work either. I will paste the code. Hope someone can find the trick.
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',
'django.core.context_processors.static',
],
},
},
]
I wrote a app named blog. I put the index.html in the ,blog/templates/blog/
views.py
def index(request):
posts = Post.objects.filter(published=True)
return render(request,'blog/index.html',{'posts':posts})
urls.py:
urlpatterns = [
url(r'^$', views.index, name='index')
]
I can't find any problems but it just does not work. It seems the template is not found but the config path is right. Or I missed something that I unaware of?
Add 'blog' to the INSTALLED_APPS tuple in settings.py
Also, below are the settings that work for me. Hope these will do the trick for you as well.
import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
PROJECT_BASE_PATH = os.path.dirname(__file__)
PROJECT_DIR_NAME = os.path.split(PROJECT_BASE_PATH)[1]
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(PROJECT_BASE_PATH, 'templates').replace('\\', '/')],
'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',
],
},
},
]
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
)