Django Rendering Template Included in a Template - python

Basically I have a base template, and the base template is including another template, let's say, latest update. So the structure is like this:
-base.html
|---------latest_update.html
So, I know how to include a simple template, like the template without any data processing (doesn't require interaction with any application view.py). However, how could I attach the included template into a django application view.py so that I could at least show updated data periodically?
I am not exactly sure terms for this, feel free to change the title.
Edit: This question is a bit cloudy, as I don't know how to put the terminology correctly. So, I have this included template. Every page will have it. So, from my limited knowledge, that means I have to render it manually for every page that hits view.py. Is there any easier way of doing this?

You can use Django templatetags. Define a file named latest_update_tags under your Django app templatetags directory and write code like this to define a latest_update templatetags:
from django import template
from app.models import UpdateObject
register = template.Library()
#register.inclusion_tag("latest_update.html")
def latest_update():
update_objects = UpdateObject.all().[:10]
return {"update_objects": update_objects}
And then in your base.html use it like this:
{% load latest_update_tags %}
......
{% latest_update %}

Related

Django project structure,

I'm new to Django and trying to understand the "correct" way to structure my project, in particular when and how to put separate functionality within an application.
The site I am building will include mainly static information (it's for a holiday cottage) which I'm providing with views from the main site project. I also want to include a calender, which will display availability in a sidebar, which I have started to build as an application "availabilityCalender" as it will be reused across most pages and I can see me using it in other projects.
What I can't figure out is how to hook this into a page view from my application. Most tutorials online approach applications as representing the entire page and link into a view from the urls.py of the project. Instead I'm want to add the application as a part of my page. The simplest way to represent what I'm trying to do is below:
from django.shortcuts import render
from django.http import HttpResponse
import availabilityCalender
def index(request):
availabilityCalender.views.monthView()
I was hoping that this would simply add the application's view into the index view. When testing this I get the error "errorAttributeError at /, 'module' object has no attribute 'views'".
Am I trying to use applications is a way the are not designed for or simply using the wrong approach? My apologies for what is almost certainly an incredibly simple question!
Edit -
I got the above code to work by changing my import:
from availabilityCalender.views import monthView
def index(request):
return monthView(request)
One way to do this would be to use template inheritance. Each one of your templates could extended a base template that includes your calendar.
Another way would be to put your calendar in its own template and to use the {% include %} template tag.
If your calendar has data associated with it, the next issue would be getting that data to the templates. If you are including the calendar on every page of your site you could create a custom context processor that automatically adds the calendar data to every request.
If the calendar is only on some pages you could just load the data on a view by view basis. Perhaps you could extract the calendar functionality into a CalendarMixin view that loads the correct calendar data in get_context_data.
first of all your project should have a structure similar to this:
-/myProject
-/myproject
-settinngs.py
-views.py
-urls.py
-/templates
-mange.py
-/availabilityCalender
-models.py
-views.py
Now you need to make sure you add to your settings INSTALLED_APPS the app "myproject.availabilityCalender", Once this is done its quite simple to use and reuse your app in all project and further apps. In this specific case if you want to import and use your app on the myproject/views.py, just do:
from myproject.availabilityCalender.models import <--MODULES YOU NEED FOR YOUR VIEW-->
def monthView(request):
.
.
.
return render(request, 'index.html')

How do I find where a django template variable comes from in the python

Is there a good general way of finding the line of python code responsible for passing in variables to django templates? When newly picking up a large code base, and I see {{ x.y }} in the template, and nothing obviously related (by how things are named) to x in the {% load ... %}, what do I do? Where can I find this variable in the python so that I can change it or related code?
My current solutions tend to be tedious and overwhelming. It's a lot of searching, but I would like to be able to just know where to look.
Look at the URL of the page. Then go to urls.py and look at which view is linked to the URL. Then open views.py and search for the view which the URL linked to.
In that view, the variable 'x' should be there. If it's not, then check the template context processors and middlewares as karthikr suggested.

How do I display static images that are in content variable in django?

I have a django site where a portion is basically a CMS. There are large content blocks (like the content of a post) held in a database and within those I need to display images that are in my static files. The content within the content block could be anything just like a regular blog post so there may be images scattered within it.
I am displaying the content in a template using the following
{{content | safe}}
I can display the images if I put the full url but that is not optimal as it doesn't use the advantages of the Django static files system. How do I go about displaying these since I can't use the {% static %} tag for the images that are within the content?
This seems like a place to use a custom filter. There are probably a lot of different ways to do it, but one could look like this:
from django import template
from my_project import settings
register = template.Library()
#register.filter
def img_link(content):
return content.replace('src=','src='+settings.STATIC_URL')
That's assuming you already have the code in your content look something like -- you can adjust the filter to meet however your images are actually written in the content. Because this is relying on the STATIC_URL from your settings, it will adjust whenever the URL is changed, exactly as {% static %} would.
of course, filters can be chained, so you could just write {{content|safe|img_link}} to activate the filter.

using django-easy_thumbnails from view

easy_thumbnails is a big help when making models or views for thumbnails.
I am using the templatetag (via template, not via model) and easy_thumbnails creates sucessfully the thumbnail files.
what happen when I want to use easy_thumbnails via view, not model o templatetag (the rendering of the images is via ajax, and django will not parse the templatetag...) for example imagine displaying infinite image thumbnails for this plugin http://sorgalla.com/projects/jcarousel/examples/dynamic_ajax_php.html . any lights? thanks!.
I'm not sure what you're asking about, but README file from easy_thumbnails should cover your use case.
And if for some reason it doesn't and you want to use {% thumbnail %} tag directly in a view, then you can always render_to_string a template that contains just: {% thumbnail %} tag, although this looks to me like a hack.

Using Django admin look and feel in my own application

I like the very simple but still really elegant look and feel of the django admin and I was wondering if there is a way to apply it to my own application.
(I think that I've read something like that somewhere, but now I cannot find the page again.)
(edited: what I am looking for is a way to do it automatically by extending templates, importing modules, or something similar, not just copy&paste the css and javascript code)
Are you sure you want to take every bit of admin-site's look & feel??
I think you would need to customize some, as in header footer etc.
To do that, just copy base.html from
"djangosrc/contrib/admin/templates/admin/"
and keep it in
"your_template_dir/admin/base.html" or
"your_template_dir/admin/mybase.html"
Just change whatever HTML you want to customize and keep rest as it is (like CSS and Javascript) and keep on extending this template in other templates of your application. Your view should provide what it needs to render (take a look at any django view from source) and you'll have everything what admin look & feel had. More you can do by extending base_site.html in same manner.
(Note: if you keep the name
'base.html' the changes made in
html will affect Django Admin too.
As this is the way we change how
Django Admin look itself.)
{% extends "admin/base_site.html" %}
is usually a good place to start but do look at the templates in contrib/admin/templates and copy some of the techniques there.

Categories