Pass url parameters to Django wizard - django

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.

Related

Instance variables in class-based generic views

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.

Is it Possible to define an alternative get_absolute_url for mobile user agent?

Is it possible to define an alternative get_absolute_url in one model? This question may seem strange but this is what I want to achieve:- I want to be able to redirect request from mobile users to a different template using Generic views without explicitly indicating the template path. I hope this is clear?
Yes, you can overload the get_absolute_url of the model by defining it in your model. It takes no arguments, but since is a python function, you can test if the user is using a mobile app to enter in your website and render the template accordingly. See it in Django documentation.
By the way, it is very normal to change the get_absolute_url() function, for instance if you have a view specific for that model, one option is the get_absolute_url() returns the reverse of that view.
Hope this helps.

Django custom view of a third party app

Lets say I have installed a 3rd party app called 'articles', the app contains basic templates and views. And there is a view called 'home' to list articles.
I need to add a form within that view and of course the form variable is not in the default 'home' view. How should I go about adding the form variable to that view?
There are a couple of ways I can think of right now:
Create another app and create a custom view.
This seems crazy and I won't do it, but for the sake of possibility, add a context processor to add the form variable into the context.
Just wondering if anyone had this situation and what is the best approach for this?
well, you may want to try a Function Decorator to redefine the previous view function.
the context processor or the middlerware is not recommend because it's global and dirty.
another use way I can thing is use-defined tags. This may takes more affort and go against the origin design of the tags. But it seem to be a good way.

Order object_list with django-profiles

Using the django-profiles app, I want to display the users' profiles in an ordered list.
Ordered by different fields, depending on the situation.
Now the docs say something about passing extra arguments to the views here but I don't know how to do that, since the urls are just included (as opposed by defined by myself).
So,how can I pass in the "order_by" part that I can just use in the normal list view?
Checking the code [1], there is no way to alter the queryset in the way you want.
Your best option is probably to write this view yourself, using the existing implementation as a guide if you like (e.g. you can still call object_list when you've got a queryset ordered to your specification). Then either override the profile list URL in your own urls.py by declaring it first:
...
url(r'^profiles/$', path.to.my_profile_list_view, name='my_profile_list'),
(r'^profiles/', include('profiles.urls')),
...
or create a new URL for this and use that on your site instead:
url(r'^ordered-profiles/$', path.to.my_profile_list_view, name='my_profile_list'),
[1] https://bitbucket.org/ubernostrum/django-profiles/src/c21962558420/profiles/views.py#cl-287
See also: https://bitbucket.org/ubernostrum/django-profiles/src/c21962558420/profiles/urls.py

Can custom Django filters access request.user?

Is it possible to access the current User (i.e. user in the template context) from a custom template filter?
Obviously I can pass the user in as an argument, but if it's possible to just grab the current user, that would be more convenient.
Django filters aren't given any special access to the context from which they are called, they're just plain old functions.
You'll need to pass in anything you want to use within the function.
https://docs.djangoproject.com/en/dev/howto/custom-template-tags/
See my answer here:
https://stackoverflow.com/a/28098279/201945
But, in short, you CAN access the context from within a custom filter by extracting it from the call stack when, and only when, the filter is called during render.
This is, admittedly, a haphazard solution. Caveat emptor.