I made in my web a menu using generic_view - simple 'django.views.generic.list_detail.object_list' in urls.py file. I would like to set a cookies each time when user chooses one of element of this list [HttpResponse.set_cookie(...)]. What is the best solution? Should I write function in views.py or have you got more simple solution?
Edit 1
This is the fragment of my urls.py:
manufacturer_dict = {
'queryset': Manufacturer.objects.all()
}
urlpatterns = patterns('',
url(r'^$', 'django.views.generic.list_detail.object_list', manufacturer_dict),
)
And template: manufacturer_list.html
<ul>
{% fo开发者_运维问答r object in object_list %}
<li><a href="{{object.get_absolute_url}}" title="{{object.name}}">{{object.name}}</a></li>
{% endfor %}
</ul>
I am only using generic_views.
This is list of mobile phone models. I want to remember the users mobile model in the cookie.
Generic views are simple views that handle a couple of common cases, for example rendering a template when no view logic is needed. In your case, you want to add functionality to your view (i.e. setting a cookie) so you will need to write your custom view. In addition, you should not add view logic in your urls.py (the queryset call), this belongs in views.py
So the process flow could look like:
1) Show mobile phone models in template using a form.
2) When user selects phone model from dropdown menu (or something similar) send phone model to a function in views.py
3) This function receives the phone model and responds with a cookie containing the phone model.
However, I am not quite sure why you would want to store the phone model in a cookie.
精彩评论