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.
Related
I want to use both jinja and Django templates together, such that it does not show error of templatenotfound. When I try to use jinja as well, Django does not view the jinja templates but only Django templates, but when I omit the Django templates configuration, jinja works fine. I want to use both.
TEMPLATES = [
# trying out jinja
{
'BACKEND': 'django.template.backends.jinja2.Jinja2',
'DIRS': [os.path.join(MAIN_DIR, 'templates_jinja')],
'APP_DIRS': True,
'OPTIONS': {'environment': 'newquiz.jinja2.environment'}
},
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates_django')],
'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',
# for redirecting to previous page after login
# imp
'material.context_processors.footer_processor',
'aboutus.context_processors.about_processor',
'quiz.context_processors.material_processor',
'social_django.context_processors.backends',
'social_django.context_processors.login_redirect',
],
},
},
]
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 need to say where the templates file is for my Django app and my user file has an apostrophe in it. Can someone help me out with this one?
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ["/Users/Harry's PC/Desktop/django_tutorials/mySite/personal"],
'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',
],
},
},
]
Then the exception type is: TemplateDoesNotExist when running the server.
I'm pretty sure you'll have to escape it with a backslash. You probably have to escape the space, too.
Of course, the easiest thing to do is rename the file without the special characters. No apostrophes and either camelcase or underscore for the spaces.
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ["/Users/Harry\'s\ PC/Desktop/django_tutorials/mySite/personal"],
'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',
],
},
},
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 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',
)