One template for multiple views

201 views
Skip to first unread message

Moreplavec

unread,
Nov 2, 2016, 6:47:54 PM11/2/16
to Django users
Greetings, 

i'm creating template for simple table report. Data shown are for current month. Than i realised i need same report for previous month. I can copy created template and modify texts, but i don't like. Is it possible to use same template for more views and only controll differences based on view, which is template calling? For example, i need to show "Current month" or "Previous month". I can put these texts into variable, but i hope there is better way to controll directly in template.

Please, can you help me or point me to some good article? Thanks!

Lekan Wahab

unread,
Nov 2, 2016, 7:28:22 PM11/2/16
to django...@googlegroups.com

Hi, 
I'm not sure I completely understand what you need but if I do,  I think passing different contexts to the same template would help you achieve that.

You could easily have something like :

def initial_view(request) :
       variableone = 'a'
       variabletwo = 'b'
       return response(yourtemplate, {'a':variableone, 'b' :variabletwo})

Then you can also have another view using the same template but with different contexts.
Something like,

def final_view(request) :
       variableone = 'c'
       variabletwo = 'd'
       return response(yourtemplate, {'c':variableone, 'd' :variabletwo})

Also,  if you have a repetitive section in your template, you could just write it once and use the {% include %} tag to use it in each of your other templates.

I hope that helps.

Lekan.
Python is the "most powerful language you can still read".


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/a61a7a9d-06a2-4749-bf71-f6d2351396af%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Moreplavec

unread,
Nov 7, 2016, 7:25:19 PM11/7/16
to Django users
Yes, i know i can pass data (variables) from view to template. But my question is, if i have report, for example with headline "This month" and same report for previous with headline "Previous month", if i can such things solve directly in template, based on view which is calling template. I don't like the way to put texts into template just to be specific for view.

But thanks for answer, it's django-way and i have to respect it :/

Dne středa 2. listopadu 2016 20:28:22 UTC+1 Lekan Wahab napsal(a):

Hi, 
I'm not sure I completely understand what you need but if I do,  I think passing different contexts to the same template would help you achieve that.

You could easily have something like :

def initial_view(request) :
       variableone = 'a'
       variabletwo = 'b'
       return response(yourtemplate, {'a':variableone, 'b' :variabletwo})

Then you can also have another view using the same template but with different contexts.
Something like,

def final_view(request) :
       variableone = 'c'
       variabletwo = 'd'
       return response(yourtemplate, {'c':variableone, 'd' :variabletwo})

Also,  if you have a repetitive section in your template, you could just write it once and use the {% include %} tag to use it in each of your other templates.

I hope that helps.

Lekan.
Python is the "most powerful language you can still read".

On 2 Nov 2016 19:48, "Moreplavec" <stanisl...@gmail.com> wrote:
Greetings, 

i'm creating template for simple table report. Data shown are for current month. Than i realised i need same report for previous month. I can copy created template and modify texts, but i don't like. Is it possible to use same template for more views and only controll differences based on view, which is template calling? For example, i need to show "Current month" or "Previous month". I can put these texts into variable, but i hope there is better way to controll directly in template.

Please, can you help me or point me to some good article? Thanks!

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.

Matthew Pava

unread,
Nov 7, 2016, 7:37:57 PM11/7/16
to django...@googlegroups.com

I would use an if block in the template, and actually, I do use an if block.

So I would have either two context variables with this_month and last_month or I would look up some filter or template tag to find the month of the current datetime object in the Django documentation.

 

<h1>

{% if month = this_month %}Current{% elif month = last_month %}Previous{% else %}{{ month }}{% endif %} Report

</h1>

ludovic coues

unread,
Nov 7, 2016, 7:38:20 PM11/7/16
to django...@googlegroups.com
You can also have a base template, with a block for the title then
extends that base template with the extends tags.
> https://groups.google.com/d/msgid/django-users/08ec103b-8f35-414e-9c94-a3ee5e398cc5%40googlegroups.com.
>
> For more options, visit https://groups.google.com/d/optout.



--

Cordialement, Coues Ludovic
+336 148 743 42
Reply all
Reply to author
Forward
0 new messages