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.
Related
I'm new to Django and I can't find anywhere which instance variables do class-based generic views have. For example, I know that you can use self.request or self.kwargs, but is there any list of which other variables can I use? Maybe I can set any variable I need in the dispatch function, but what are the defaults?
I agree that this is not documented as well as it could be.
The overview of class-based generic views says this:
Various useful things are stored on self; as well as the request (self.request) this
includes the positional (self.args) and name-based (self.kwargs) arguments captured
according to the URLconf.
Then, the reference documentation lists the attributes created for each specific view. Under DetailView, for example, it says:
While this view is executing, self.object will contain the object that the view is
operating upon.
Another reference you might want to consult is Classy Class-Based Views.
I am reading:
https://docs.djangoproject.com/en/dev/topics/http/shortcuts/#render
If I understand correctly context_instance is automatically created from the first parameter (request).
How are these values presented to the view? Does it combine them with the dictionary parameters?
What types of things are normally in the context_instance? (I know it is built from request).
I have also read that template pre processors can add data to context_instance.
I am just trying to understand how I can use context_instance and have been searching and haven't quite found the right answer
Template context processors are the only things that affect the context in any way from a render call. All of these - either the default ones, which add things like user, or your own custom ones - simply add elements to the context dictionary.
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.
I'm building a database application using django. Much of the data recorded requires supporting documentation (this documentation is scanned in and uploaded). Many of my django views include links to my scanning view, and arguments are passed into that view. In fact the view that handles the scanning takes 9 optional kwargs. I can't work out how to set up my urls.py so as to handle the following:
HttpResponseRedirect(reverse('general_doc_upload', kwargs = doc_parameters))
I'm sure there must be a nicer way of handling this than trying to write Regex for every possible combination of kwargs.
Unfortunately the I don't have a lot of leeway with the underlying database structure, this has been specified by the client, the django models (and corresponding views) have been written to fit this structure.
This sort of thing is where putting the parameters in the URL breaks down. Instead, you should pass them as GET parameters - /my/url/upload/?param1=foo¶m2=bar etc.
In your urlconf, just match the basic pattern with r'upload/$', and get the parameters in your view with request.GET['param1'] etc.
I want to inject one additional variable into the context of a view that belongs to a 3rd-party application without editing the code of that application. Is there a way to do this by wrapping the (non-generic) view? It doesn't accept an extra_context parameter, so the approach described in this SO thread won't work. I know I could create a context processor, but that seems like a lot of overhead, having this variable available on every page when I just want it in one view. Or is there a way to narrow the scope of a context processor?
I think a context processor is the way to go, but with a splash of logic in there that checks the request path (for instance) and only bothers digging out and setting the variable for the occasions when it's needed