django-class-based-views

How to process a form (via get or post) using class-based views?

邮差的信 提交于 2019-12-20 08:02:24
问题 Im trying to learn class-based views, for a detail or list view is not that complicated. I have a search form and I just want to see if I send a query to show up the results. Here is the function code (is not mine, is from a django book): def search_page(request): form = SearchForm() bookmarks = [] show_results = False if 'query' in request.GET: show_results = True query = request.GET['query'].strip() if query: form = SearchForm({'query': query}) bookmarks = Bookmark.objects.filter(title_

Django LoginFormMiddleware breaks with class based views

十年热恋 提交于 2019-12-20 06:29:50
问题 As per a few other SO answers, I'm using middleware to show a login form on every page of my project, such that a user can login in-place. I am aware some frown upon this, but it really does make for a much better user experience. Ideally it'd be asynchronous, but I haven't gotten there. Anyway, middleware.py: from MyProj.forms import MyProjTopLoginForm from django.http import HttpResponseRedirect from django.core.urlresolvers import reverse #Notes: https://stackoverflow.com/questions/2734055

How do I use UpdateView?

爷,独闯天下 提交于 2019-12-18 05:07:10
问题 I have two, presumably related, problems with UpdateView. First, it is not updating the user but creating a new user object. Second, I cannot restrict the fields displayed in the form. Here is my views.py: class RegistrationView(FormView): form_class = RegistrationForm template_name = "register.html" success_url = "/accounts/profile/" def form_valid(self, form): if form.is_valid: user = form.save() user = authenticate(username=user.username, password=form.cleaned_data['password1']) login(self

cache_page with Class Based Views

那年仲夏 提交于 2019-12-18 01:32:14
问题 I'm trying to do cache_page with class based views (TemplateView) and i'm not able to. I followed instructions here: Django--URL Caching Failing for Class Based Views as well as here: https://github.com/msgre/hazard/blob/master/hazard/urls.py But I get this error: cache_page has a single mandatory positional argument: timeout I read the code for cache_page and it has the following: if len(args) != 1 or callable(args[0]): raise TypeError("cache_page has a single mandatory positional argument:

Django, name parameter in urlpatterns

那年仲夏 提交于 2019-12-17 22:33:48
问题 I'm following a tutorial where my urlpatterns are: urlpatterns = patterns('', url(r'^passwords/$', PasswordListView.as_view(), name='passwords_api_root'), url(r'^passwords/(?P<id>[0-9]+)$', PasswordInstanceView.as_view(), name='passwords_api_instance'), ...other urls here..., ) The PasswordListView and PasswordInstanceView are supposed to be class based views. I could not figure out the meaning of the name parameter. Is it a default parameter passed to the view? 回答1: No. It is just that

Updating User model in Django with class based UpdateView

不打扰是莪最后的温柔 提交于 2019-12-17 22:33:24
问题 I am trying to update the Django User model with the class based UpdateView that automatically renders with the current user but am getting an error that a pk or slug is required. The form work and renders with the proper current user context but throws the error when I try to submit the changes. Below is the view I am using: class UserUpdateView(UpdateView): form_class = UserForm model = User template_name = 'members/user_update.html' def get(self, request, **kwargs): self.object = User

What is the advantage of Class-Based views?

半城伤御伤魂 提交于 2019-12-17 21:38:58
问题 I read today that Django 1.3 alpha is shipping, and the most touted new feature is the introduction of class-based views. I've read the relevant documentation, but I find difficult to see the big advantage™ that I could get by using them, so I'm asking here for some help in understanding them. Let's take an advanced example from the documentation. urls.py from books.views import PublisherBookListView urlpatterns = patterns('', (r'^books/(\w+)/$', PublisherBookListView.as_view()), ) views.py

Django UpdateView without pk in url

不问归期 提交于 2019-12-17 18:38:39
问题 Is it possible eliminate pk from url related to UpdateView ? For example, if I have url(r'^myobj/update/(?P<pk>\d+)/$', views.UpdateMyObj.as_view(), name="update") is there any way to write it like url(r'^myobj/update/$', views.UpdateMyObj.as_view(), name="update") and then send pk as a parameter in POST or GET request? 回答1: Yes it is possible you just need to override the get_object method: from django.views.generic.edit import UpdateView class UpdateMyObj(UpdateView): # ..... def get_object

Updating context data in FormView form_valid method?

瘦欲@ 提交于 2019-12-17 10:46:46
问题 I have a class QuestionView which is derived from the FormView class. Here is a code snippet to explain my problem: class QuestionView(FormView): ... context_var1 = y def form_valid (self, form): ... self.context_var1 = x ... def get_context_data(self, **kwargs): ... context['context_var1'] = self.context_var1 ... return context As shown above, I update a set of context variables in form_valid and I intend to use the updated values of these in the template - hence the variables in the context

Django: difference between is_valid and form_valid

混江龙づ霸主 提交于 2019-12-13 13:39:52
问题 I've created a form which is a forms.ModelForm . On the "view" side, I've created a view which is a generic.UpdateView . In those 2 differents classes, I have is_valid() on one side, and form_valid() on the other side. class ProfileForm(FormForceLocalizedDateFields): class Meta: model = Personne fields = ('sexe', 'statut', 'est_fumeur', 'est_physique', 'date_naissance') exclude = ('user', 'est_physique') # blabla fields declaration def is_valid(self): pass and edit view: class EditView