using normal html files instead of Django templates - django

I have a bunch of html files. I'd like to render them in my Django View without converting them to Django templates. Is this possible?
Or is there any code to do the conversion easily?

Actually, you don't have to write a view for such cases. You can use direct_to_template shortcut in urls.py as is.
From generic views doc
from django.conf.urls import patterns, url, include
from django.views.generic.simple import direct_to_template
urlpatterns = patterns('',
('^about/$', direct_to_template, {
'template': 'about.html'
}),
)

It is possible, just use the following method call:
def main_page(request):
template=get_template('index.html')
variables=Context({})
output = template.render(variables)
return HttpResponse(output)
Here your index.html file is located in templates folder.

Django templates are built on top of plain old files. If you have HTML files that don't have {{ or {% in them, then they are automatically valid Templates, albeit consisting of a single Text node.
Just reference them as you would reference any template, and it will "just work".
return render(request, "my-plain-html-file.html")

Related

Can't figure out why django variables aren't passed into template

I've been working on this for a few hours and for the life me of I can't get a variable from my django app into a template.
html:
{% block content %}
{{test}}
{% endblock content %}
views:
def home(request):
context = {'test' : "test"}
return render(request, "/dbus/templates/index.html", context)
urls.py:
from django.urls import path
from . import views
urlpatterns = [
path('', views.home, name = 'home')
]
(note: I left the imports out of the view code sample I provided)
I think you dont need to add 'templates' in the argument "/dbus/templates/index.html"
If dbus is your django-app and templates is a folder inside this django-app, then by convention, you've to add a subfolder named dbus inside this templates folder.
All HTML files regarding the django-app 'dbus' should be stored inside this subfolder named 'dbus' (which comes inside templates folder)
The path of index.html should looks like: YourProjectFolder/dbus/templates/dbus/index.html
If you're right till this part, then you've to render the html page like "dbus/index.html"
You dont need to add that templates directory in the render argument.
So correct line is: return render(request, "dbus/index.html", context)
This should solve your problem!
Turn's out the issues was due to caching in either Nginx or uWSGI. I restarted both and the changes started working

Django Caching on front-end

I was working with 2 applications that are within a DJango project: "customer" and "vendors". Each application has a HTML file named "testindex.html".
Whenever I typed:
http://myhost/customer/basic_info
the correct page would show up
If I typed
http://myhost/vendors/basic_info
the page from http://myhost/customer/basic_info would show up
I found out that it was due to caching (since both applications use "testindex.html"). So again, "testindex.html" is caching.
How can one get around this problem?
TIA
Details are listed below. I have the following views defined:
urls.py for the project
urlpatterns = [
... snip ...
url(r'^customer/', include('libmstr.customer.urls')),
url(r'^vendors/', include('libmstr.vendors.urls')),
]
views.py for customer
from django.shortcuts import render
def basic_info(request):
return render(request, 'testindex.html', {})
views.py for vendors
from django.shortcuts import render
def basic_info(request):
return render(request, 'testindex.html', {})
urls.py for customers
from django.conf.urls import url
from . import views
# list of templates
app_name = 'customer'
urlpatterns = [
url(r'^basic_info/$', views.basic_info, name='basic_info'),
]
urls.py for vendors
from django.conf.urls import url
from . import views
# list of templates
app_name = 'vendors'
urlpatterns = [
url(r'^basic_info/$', views.basic_info, name='basic_info'),
]
It sounds like you have two templates, customers/templates/testindex.html and vendors/templates/testindex.html.
When you call render(request, 'testindex.html', {}), the app directories template loader searches the templates directory for each app in INSTALLED_APPS, and stops the first time it finds a match. If customers is above vendors in INSTALLED_APPS, then it will always use the customers template.
For this reason, Django recommends that you name your templates customers/templates/customers/testindex.html and vendors/templates/vendors/testindex.html, and change your views to use customers/testindex.html and vendors/testindex.html. This way you avoid clashes.

Is there a way to render a html page without view model?

Is there a way to render a html page without having a view model in django if a page is going to display only static html?
Also, can I redirect to a html page instead of a url? For example, instead of doing this:
return HttpResponseRedirect('form/success/')
can I do this:
return HttpResponseRedirect('success.html')
?
direct_to_template no longer works in Django 1.8. Here is how to do it in 1.8, in the urls.py:
from django.views.generic import TemplateView
urlpatterns = [
url(r'^$', TemplateView.as_view(template_name="your_static.html"), name='whatever'),
]
You can render a template without a view using the generic view direct_to_template.
Redirecting to success.html isn't the way to go in django since success.html doesn't have an associated URL. You always have to bind the template to an url via a view (direct_to_template is a view which gets all its arguments from the conf in urls.py, but it's still a view)
For static HTML you could use Flatpages app.
You could also serve rendered templates (which could contain only static HTML) but you will need a view:
from django.shortcuts import render_to_response
def some_view(request):
return render_to_response('sometemplate.html')
About redirection, basically you can't redirect to HTML page by just giving the filename, even if it were statically served by the web server you would still be using a URL that points to that file.
I'm pretty sure this isn't what OP wanted to do, but if someone wants to render something without a view, (for sending an email or appending to other html code), you can use this:
from django.template.loader import render_to_string
html = render_to_string('template.html', params)
Use lambda function.
urlpatterns = [
path('', lambda request: render(request, 'homepage.html'))
]

Django can't find HTML file

I have a html file ('search.html') with a form on it. I have saved it to ~/Django/Templates just for the sake of argument. The Django book said it doesn't matter where I save it, because the framework will find it. Anyway, I have set up a function in the views.py file to render this file. Here it is:
from django.http import HttpResponse
from django.shortcuts import render_to_response
def search(request):
return render_to_response('search.html')
I have this function called in the urls.py file as well:
urlpatterns = patterns('',
(r'^$', index),
(r'^search/$', search),
However, whenever I go to visit the page with the ~/search in the URL, I get the following:
TemplateDoesNotExist at /search/
What's the problem?
In your settings.py file there is a line...
TEMPLATE_DIRS = (...)
You want to make sure the directory containing the template is in that tuple.

Django static page?

I want to make a static page which will be shown to the user only if he/she clicks on a link provided in one of my models. I can do this by making a Python page alone and calling it, but I want it be called from Django. The user interface should be constructed using the Django API only.
Any suggestions?
With the class-based views in newer Django versions, one can use this in urls.py:
from django.views.generic import TemplateView
url(r'^about',
TemplateView.as_view(template_name='path/to/about_us.html'),
name='about'),
Bypassing views to render a static template, add this line in "urls.py". For example "About Us" page could be
(r'^about', 'django.views.generic.simple.direct_to_template', {'template': 'path/to/about_us.html'}),
Do you mean something like Django's flatpages app? It does exactly what you describe.
If you want to make a static page the flatpages is a good choice. It allows you to easily create static content. Creating static content is not harder than creating a view really.
On Django 2.2.6, loosely following David's answer, I added the path in urls.py:
from django.views.generic import TemplateView
urlpatterns = [
.... .... ....
path('about',
TemplateView.as_view(template_name='path/to/about_us.html'),
name='about'),
And I needed to adjust settings.py to specify the template directory:
TEMPLATES = [{
... ... ...
'DIRS': [os.path.join(BASE_DIR, 'template')],
Then I saved the actual content in template/path/to/about_us.html