django-forms

How to preserve form fields in django after unsuccessful submit?

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-13 10:43:05
问题 Code from views.py: def feedback(request): if request.method == "POST": form = CommentForm(request.POST) if form.is_valid(): form.save() else: print("form.errors:", form.errors) else: form = CommentForm() articles = Comment.objects.all() ResponseDict = {"articles": articles, "form": form} return render_to_response("feedback.html", ResponseDict, context_instance = RequestContext(request)) I've tried this and several modifications from answers to similar questions, but nothing works. When I

django choiceField with CheckboxSelectMultiple: all selected by default?

佐手、 提交于 2020-01-13 07:52:09
问题 I'm using a choiceField with the CheckboxSelectMultiple widget. Is it possible to render all checkboxes as checked by default? Thanks! 回答1: Just set the initial values from the field's choices, like this: MY_CHOICES = ( ("some", "Some choice"), ("another", "Another choice"), ("best", "Best choice") ) ... multiple_choice = forms.MultipleChoiceField( label=u"Select multiple", choices=MY_CHOICES, widget=forms.widgets.CheckboxSelectMultiple, initial=(c[0] for c in MY_CHOICES) ) 回答2: I am doing

Django admin site: How does the Add User page work (more fields on edit)?

南笙酒味 提交于 2020-01-13 07:00:28
问题 I was wondering how they made it possible to display more fields in the User page of the Django admin site. If you create a new User you only have some basic fields to fill in, but if you reopen that user (edit mode) then you see a lot more fields to fill in. I'm trying to achieve the same, I had a look at the add_form.html template but I can't really get my head around it. I guess I'm looking for a way of specifying different fields = [] sets based on the edit status of the document. Thanks!

customize the way usercreationform looks in django

纵然是瞬间 提交于 2020-01-13 03:45:06
问题 I want to customize the UserCreationForm from django. I do the following class myUserCreationForm(UserCreationForm): class Meta: model=User fields = ('username', 'password1', 'password2') widgets = { 'username':TextInput(attrs={'class':'form-control'}), 'password':TextInput(attrs={'class':'form-control'}), 'password2':TextInput(attrs={'class':'form-control'}), } but its not working. When the template is rendered It creates the input boxes don't have the form-control class attached to them.

Django form with just a BooleanField

你离开我真会死。 提交于 2020-01-12 14:03:15
问题 I'm rather new to Django and I'm using Django 1.0. I have this: forms.py: class MyForm(forms.Form): extra_cheeze = forms.BooleanField(required=False, initial=False, label='Extra cheeze') views.py: def order_something(request): form = MyForm(request.POST or None) if request.method == 'POST' and form.is_valid(): # do stuff... The problem is that the form is not valid unless the checkbox is checked, so there doesn't seem to be a way to get a False value from the field. As far as I can understand

Storing file content in DB

旧时模样 提交于 2020-01-12 07:12:02
问题 I am making a model in which i have a FileField . I want to store the file content in a database column, instead of file path. Any suggestions? 回答1: Disregard the naysayers. If you want to have full control over your content, put the files in a blob field in the database. I generally also keep the filename in a separate field, so I can reconstruct the file as necessary (that way you keep the extension, which ties it to a file type in most operating systems). Be sure to store the actual blob

Django ModelChoiceField - use something other than id?

ε祈祈猫儿з 提交于 2020-01-12 07:09:55
问题 Say I have an address table and it has a postal_code field -- ModelChoiceField does not allow me to use something other than PKs to validate existence correct? What would be the way to go? Normal input and use clean_*() ? 回答1: What about to_field_name ? I'm not sure if it's documented anywhere, but you can find it easily between ModelChoiceField constructor params: https://github.com/django/django/blob/master/django/forms/models.py. It is used to filter field queryset. For example: articles =

Django ModelChoiceField - use something other than id?

大城市里の小女人 提交于 2020-01-12 07:09:48
问题 Say I have an address table and it has a postal_code field -- ModelChoiceField does not allow me to use something other than PKs to validate existence correct? What would be the way to go? Normal input and use clean_*() ? 回答1: What about to_field_name ? I'm not sure if it's documented anywhere, but you can find it easily between ModelChoiceField constructor params: https://github.com/django/django/blob/master/django/forms/models.py. It is used to filter field queryset. For example: articles =

Django ModelChoiceField - use something other than id?

天涯浪子 提交于 2020-01-12 07:09:06
问题 Say I have an address table and it has a postal_code field -- ModelChoiceField does not allow me to use something other than PKs to validate existence correct? What would be the way to go? Normal input and use clean_*() ? 回答1: What about to_field_name ? I'm not sure if it's documented anywhere, but you can find it easily between ModelChoiceField constructor params: https://github.com/django/django/blob/master/django/forms/models.py. It is used to filter field queryset. For example: articles =

Django pass object from view to next for processing

会有一股神秘感。 提交于 2020-01-12 05:25:17
问题 If you have 2 views, the first uses modelform that takes inputted information from the user (date of birth, name, phonenumber, etc), and the second uses this information to create a table. How would you pass the created object in the first view to the next view so you can use it in the second view's template I'd appreciate any help you can share 回答1: One approach is to put the object into a session in your first view, which you could then retrieve from the request.session in the second view.