Does gin support global context for html render? - go-gin

I have a case where auth name variable will be used in all html templates, so how to support this without calling it each time

Related

Pass url parameters to Django wizard

As the title implies, is there a way to pass url parameters to a Django wizard?
For example, in a normal view you can use the *some_parameter* value of the urlpattern:
r'^blah/(?P<some_parameter>\d{8})/$
Is it possible to do the same when there is a wizard at the specified address? Perhaps add it to the initial data dictionary for each form?
If this view is based on regular Django class-based views it should take any URL arguments you pass to it, and make it accessible to its methods under self.args and self.kwargs.
See example for ListView in Django docs. In your case you'd see the argument in self.kwargs['some_parameter'].
Many of the generic class based views offer this functionality probably the most extensible one to capture any value that you could use would be TemplateView(). Sub-class TemplateView() and it will give you access to a context object name called params. It will be populated with variables from the url that invokes TemplateView() which it parses any variable parameters you give it.
The above answer is the most correct answer for a single model based list.

Differentiate Context, Request Context in django

What is the difference between Context, Request Context in django?
Why do we need context processors?
RequestContext simply goes through your TEMPLATE_CONTEXT_PROCESSORS setting and adds variables in addition to the ones you explicitly pass to the context class.
The context processors are literally just a function that accepts request as the first argument and returns a dictionary to be added into the context.
Why do you need them? Because some very common operations like adding the currently logged in user or STATIC_URL variables to the context would get highly repetitive if not automated.

Best Practice for Context Processors vs. Template Tags?

In which cases is it better to create template tags (and load them into the template), than creating a context processor (which fills the request automatically)?
e.g. I have a dynamic menu that has to be included into all templates, so I'm putting it into my base.html. What is the preferred usage: context processor or custom template tag? And why?
Context processors is for putting data (information, content, objects) into the context used to render page.
Template tags are for formatting or processing that content.
A template tag that makes up new data is confusing. Not impossible or wrong, but very confusing.

Dynamic templates in Django

I'm about to make a dynamic website in Django so that user can change the template the website is based upon....for new users, they'll have to choose a template they like from a menu, after which this is stored in a cookie for future reference. I however don't know how to do this....any ideas.
Templates are text files in a directory on your server.
The menu is a list of directories.
The specific template to be loaded is named in your render_to_response calls in each view function.
It's wonderfully quite simple and elegant.
You never need to do a template "swap". You can simply have all the templates you ever want in a template search path. All can be available to all users at all times. You just provide directory_name/template_name. All handled for you.
Don't mess with explicit cookies. Django already does this for you. See chapter 12 of the Django Book.
Use the Profiles extension and put the selected template directory name in the user's profile.
When user selects a template, store the template name/ some sort of alias in the cookie. When the user loads the page again, in the view function, check for template identifier in the cookie. If the identifier is present, retrieve the actual template path and pass it to the render_to_response function.

How can I put a block of dynamically generated content into a django template?

I want to include things like twitter status, or delicious tags, in my django templates.
These things are dynamic, yet regular. How would this be done?
There are a number of ways to handle this, so you can choose a method that best matches your own personal style or requirements:
Template context variable: as answered by Alex you can put your content into a context variable that is included in the context of every template created by every view. Django even provides a mechanism for doing this automatically, called a context processor. Pros: very straightforward. Cons: won't dynamically refresh new content on client browsers.
AJAX dynamic loading: as mentioned by Alex and Dave you can dynamically load your content using AJAX methods. As an example using jQuery, you would put a placeholder in your template something like <div id="twitterfeed"></div> and then in a javascript block in your template put $("#twitterfeed").load("{% url twitterfeed %}"); where twitterfeed is a url so named in your urls.py. Pros: will dynamically update browsers. Cons: can be tricky if you don't know Javascript.
Inclusion tag: Django provides a type of template tag called an inclusion tag, which is basically a custom template tag that can render dynamic content. In a way it's similar to a context variable, except your code to generate the content will only be called when you use the custom template tag in your template instead of being called for every view. Another benefit is the content is generated from a template of its own. You could do this with a normal context variable of course, but it's not as clean (IMHO) as using an inclusion tag. Pros: very straightforward, clean. Cons: won't dynamically refresh new content on client browsers.
The simplest approach is to use {{ mycontent }} in your template (where you want the dynamically generated content to appear) and put the correspondence between mycontent and its value in the context you use to render the template -- i.e., the most fundamental part of django's templating.
If what you mean is that you want Ajax support whereby Javascript on the page continuously refreshes such content according to what the server wants it to be at any given time, I suggest looking into dojango, the Dojo/Django integration project -- it's not yet as fully mature as each of Dojo and Django are on their own (not version 0.4 yet), but it is already usable and useful.
A common technique is to leave a placeholder div in the generated content, then fill the div in on the client side via an AJAX call from Javascript that you include in the page.
That gives you the benefit of having a cacheable (fast loading) primary page, with separate dynamic bits. Depending on how live you want the dynamic bits, you can can even cache them for shorter durations.