I'm trying to render to a template from another app but not sure how to import or if I should be importing with templates?
The structure is
project->
main app->
templates->
pages->
home.html
account ->
current app->
views.py
my views.py file sits within the current app and is trying to access templates within the main app (in the pages subfolder).
How would I render to it:
def temp_view(request):
....
return render(request, "home.html",context)
At first you should have most probably configure your templates static path in settings.py with something similar to this
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.template.context_processors.i18n',
],
},
},
]
APP_DIRS=True means Django will look for templates in each app directory in your case main_app/ and current_app/:
you have simply to mention the template path considering this as root path, so simply:
def temp_view(request):
....
return render(request, "pages/home.html",context)
Django will look under main_app/ directory to find the file pages/home.html
Would it be simpler to create the view in the same app you would like to render?
from mainapp.models import mainappmodel
def currentappview(request):
all = mainappmodel.objects.all()
return render(request, "pages/home.html", {'all': all})
Related
My index is loading well on browser, but when I click home page, there is nothing 404 error, is responding static/index.html, (when I click home page or any other such as contact, it is searching for html in static) why is it asking for index.html in static files and how can I rectify that?[
I stored my html files on templates folder thinking that when i clicked my dropdown, they were going to apper on my web.
if you create your index.html file on templates/index.html,then you can use this settings on your settings.py file:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
# Add 'TEMPLATE_DIR' here
'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',
],
},
},
]
then in your view you need to render that file:
def home(request):
return render(request, 'index.html', context=context)
You have to use a TemplateView. It is just a simple view that serve a template html.
from django.views.generic import TemplateView
class IndexView(TemplateView):
template_name = "PATH_TO_INDEXHTML"
Just add an url in urls.py for serving this view and let's go
from django.urls import path
from .views import IndexView
urlpatterns = [
path('index', IndexView.as_view(),
]
More information about templateview: https://docs.djangoproject.com/fr/4.1/ref/class-based-views/base/#django.views.generic.base.TemplateView
I'm new to django.
The error is coming
templateDoesNotExist at /
There is one app in my project,I've put the template folder in the app
Structure is like:
Project_name , manage.py,app_name,SQLite
The structure of the app (app_name):
other files...
Inside template folder .. (Templates(folder) -> app_name(folder) ->HTML file)
What setting I've to do in settings.py of prj to debug this error.
Did I've to include path of every template of each app. ?
Like inside setting.py in Templates
DIRS: ['BASE_DIR', "path"]
======== check & verify this ========
step-1 : check your app is registered or not
step-2: if your [templates] folder is in the app then go with the below setting otherwise put the path of your template in DIRS:['your template directory']
=== this is the default setting for a template if your templates folder in your application folder ===
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',
],
},
},
]
step-3: check in views.py template name is correct or not
I'm thinking in organizing all my templates in inside my project: scolarte.
As suggested by this question:
What is the best location to put templates in django project?
If you can’t think of an obvious place to put your templates, we
recommend creating a templates directory within your Django project
(i.e., within the mysite directory you created in Chapter 2, if you’ve
been following along with our examples).
But I need to call it from another app. The view is called but getting error:
TemplateDoesNotExist at /cuentas/ingreso/
scolarte/templates/scolarte/registration/signup.html
I even tried to put the full path to the template in project folder:
roles/views.py:
class SignUpView(TemplateView):
template_name = 'scolarte/templates/scolarte/registration/signup.html'
# don't work neither
#template_name = 'templates/scolarte/registration/signup.html'
#template_name = 'scolarte/registration/signup.html'
#template_name = 'registration/signup.html'
roles/urls.py:
from django.urls import include, path
from .views import SignUpView, SellerSignUpView, ClientSignUpView
urlpatterns = [
path('ingreso/', SignUpView.as_view(), name='signup'),
]
scolarte/urls.py
urlpatterns = [
path('', include('core.urls')),
path('cuentas/', include('roles.urls')),
path('admin/', admin.site.urls),
]
My app is orgnized like this:
roles
|_migrations
|_templates
...
|_urls.py
|_views.py
scolarte #project name
|_templates
|_scolarte
|_registration
|_signup.html
|_setting.py
|_urls.py
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',
],
},
},
]
UPDATE 1:
roles app:
** roles app - view.py **:
UPDATE 2:
Your path to the template is incorrect by standard Django convention, but let me show you first how to fix it. What you'll want to do is make sure in settings.py you have these settings made. This is from a Django 3.0 fresh project creation.
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
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',
],
},
},
]
If you use those, and follow the suggested directory structure of:
scolarte
|_scolarte
|_settings.py
|_urls.py
(etc..)
|_templates
|_scolarte
|_registration
|_signup.html
Then you can use with this path:
template_name = 'scolarte/registration/signup.html'
I have encountered interesting problem on Django 2.
In my settings.py I have written this:
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',
],
},
},
]
After that I have created folder templates in my projects root folder.
Then I have created app with name front and added it into my settings.py INSTALLED_APPS:
Inside my views.py I have added this view:
def index(request):
return render(request, 'front/index')
I have assigned its url like this:
url(r'^$', views.index, name='index'),
When I'm trying to access to this view I get this error:
TemplateDoesNotExist at /
front/index
Why this happening? Did I miss something?
Change
def index(request):
return render(request, 'front/index')
to
def index(request):
return render(request, 'front/index.html')
I tried to use render to show the HTML edited on PyCharm, but when I entered the address: 127.0.0.1:8000/index/, the following TemplateDoesNotExist exception appeared:
TemplateDoesNotExist at /index/
index.html
Request Method: GET
Request URL: http://127.0.0.1:8000/index/
Django Version: 1.11.1
Exception Type: TemplateDoesNotExist
Exception Value:index.html
Exception Location: D:\python3.6.1\lib\site-packages\django\template\loader.py in get_template, line 25
Python Executable: D:\python3.6.1\python.exe
Python Version: 3.6.1
Python Path:
['C:\Users\Administrator\guest',
'D:\python3.6.1\python36.zip',
'D:\python3.6.1\DLLs',
'D:\python3.6.1\lib',
'D:\python3.6.1',
'D:\python3.6.1\lib\site-packages']
Server time: Fri, 2 Jun 2017 03:30:58 +0000`TemplateDoesNotExist at /index/
settings.py:
ROOT_URLCONF = 'guest.urls'
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',
],
},
},
]
views.py:
from django.shortcuts import render
# Create your views here.
def index(request):
return render(request,"index.html")
Screenshot of PyCharm:
I have tried some methods provided on StackOverflow, but they do not work. Is this exception caused by the wrong dirs? How can I deal with such situation?
Look at 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',
],
},
},
]
Specifically
'DIRS': [],
You need to tell Django where to look for your templates. If you have a templates folder in your main project root. Then, add
os.path.join(BASE_DIR, 'templates')
This assumes your project has this structure:
root
app1
...
templates/
what we're referencing with os.path.join(BASE_DIR, "templates")
....
static/
what we're referencing with STATIC_ROOT = os.path.join(BASE_DIR, "static")
...
If you are placing templates inside of your apps. There are two things.
Your app inside your project will need this structure for the template folder
app name (folder)
templates (folder)
app name (folder)
(template files) e.g. - "base.html"
You will need to add this line
os.path.join(BASE_DIR, APP NAME, "templates")
Addendum: Undoubtedly you will run into this problem as well... so let's go ahead and cover it.
If you are extending / including templates which are in an app (not in the base template folder) you will need to reference where they are located:
Example: I have a template in "myapp/templates/myapp/template.html" and I want to include it.
Then, I'd do {% include "myapp/template.html" %} or if extending {% extend "myapp/template.html" %}. If they are in the base template folder then you can just reference them directly as "template.html"
I have the same issue. It was mainly caused if you didn't install your app in "settings.py" file
Just do this in your settings.py file and your error will be gone
INSTALLED_APPS = [
'<app name>.apps.<app name>Config',
]
In the place of paste your "app name"