21đź‘Ť
You are not calling the render
method there, are you?
Compare:
template.render
template.render()
37đź‘Ť
If your file isn’t a django template but a plain html file, this is the easiest way:
from django.shortcuts import render_to_response
def index (request):
return render_to_response('app/index.html')
UPDATE 10/13/2020:
render_to_response
was deprecated in Django 2.0 and removed in 3.0, so the current way of doing this is:
from django.shortcuts import render
def index (request):
return render(request, 'app/index.html')
- [Django]-Django: Calculate the Sum of the column values through query
- [Django]-How to see details of Django errors with Gunicorn?
- [Django]-Django : Unable to import model from another App
16đź‘Ť
If your CSS and JS files are static don’t use Django to serve them, or serve them as static files
For your html you could do the same if it is just some fixed file that won’t have any dynamic content. You could also use generic views with the TemplateView, just add a line like this to your urls.py
:
url(r'^path/to/url', TemplateView.as_view(template_name='index.html')),
- [Django]-Filtering using viewsets in django rest framework
- [Django]-How do I make many-to-many field optional in Django?
- [Django]-Race conditions in django
13đź‘Ť
By using HttpResponse
you can send data only, if you want to html file then you have to use render
or render_to_response
by following ways…
from django.shortcuts import render
def index(request):
return render(request, 'app/index.html')
or
from django.shortcuts import render_to_response
def index (request):
return render_to_response('app/index.html')
- [Django]-How to chcek if a variable is "False" in Django templates?
- [Django]-What is related_name used for?
- [Django]-CSV new-line character seen in unquoted field error
3đź‘Ť
First, you need to do some settings for templating:
Create “templates” folder in the root directory, where manage.py is located after you created your project.
Then go to settings.py , in the TEMPLATES section enter the directory of templates folder. Hard coding is not recommended because if you send your project to your friend, your friend won’t be able to run it. Should be like 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',
],
},
},
]
Assuming that you have home.html and about.html have in templates folder:
urls.py
from django.contrib import admin
from django.urls import path
from . import views
urlpatterns = [
path('admin/', admin.site.urls),
path('about/',views.aboutview),
path('',views.homeview),
]
views.py
from django.http import HttpResponse
from django.shortcuts import render
def aboutview(request):
return render(request,"home.html")
def homeview(request):
return render(request,"about.html")
- [Django]-Using only the DB part of Django
- [Django]-Uncaught TypeError: $(…).datepicker is not a function(anonymous function)
- [Django]-Custom ordering in Django
2đź‘Ť
from django.http import HttpResponse
from django.template import loader
def index(request):
template=loader.get_template('app/index.html')
return HttpResponse(template)
- [Django]-Error: No module named psycopg2.extensions
- [Django]-Django data migration when changing a field to ManyToMany
- [Django]-How to escape {{ or }} in django template?