django-forms

Django: How to populate form data in template

爱⌒轻易说出口 提交于 2019-12-20 06:27:49
问题 So I am trying to have "two forms" in single class based view. My view is: class ListAndCreate(CreateView): model = xmpp_buddy_groups form_class = xmpp_buddy_groups_form second_form_class = sip_extension_form template_name = "xmpp/index.html" success_url = reverse_lazy('xmpp:index') def get_context_data(self, **kwargs): context = super(ListAndCreate, self).get_context_data(**kwargs) context['object_list'] = self.model.objects.all() extension = SipExtension.objects.values_list('sip_extension',

ajax with django forms

萝らか妹 提交于 2019-12-20 06:26:26
问题 can i add the ajax code with django? i have created a simple registraion form that have 5 fields . i wish to disply the each fields in different pages but in a single window . it means by using next button 5 pages want to disply in a single window. same time all content of each page i want add to my database. is this possible in django with ajax.. my codes are as follows : #view from django.shortcuts import render_to_response from registration.models import UserDetails from forms import

Disable choice list in Django admin, only for editing

Deadly 提交于 2019-12-20 05:46:08
问题 I want to disable some fields when I am editing an object. I have managed to do this for text fields, but it's been impossible for a dropdown list (choice list). I am doing this action in the constructor of the form. class OrderModelForm(forms.ModelForm): def __init__(self, *args, **kwargs): super(forms.ModelForm, self).__init__(*args, **kwargs) instance = getattr(self, 'instance', None) if instance and instance.pk: self.fields['description'].widget.attrs['readonly'] = True self.fields['city

Django ModelForm ChoiceField not displaying instance data

纵饮孤独 提交于 2019-12-20 05:25:11
问题 I have a ModelForm class in which I set a couple of the fields as ChoiceField . For one of my views, I'd like to create a form from my ModelForm class that pulls from an instance of my model in the database (like so): form = MyModel(instance=model_instance) When I do this and then render the form in a template, I've noticed that most of the fields are pre-populated with values pulled from the model instance, which is what I want. However, this isn't the case for two ChoiceField fields. These

form.is_valid() always returning false

删除回忆录丶 提交于 2019-12-20 04:47:32
问题 Form.is_valid() is always returning false, and it never shows any kind of error. Following is the code snippet from forms.py, views.py and template/transaction_add_page.html Form class TransactionForm(forms.Form): account = forms.ModelChoiceField(queryset=Account.objects.none()) transactions = forms.CharField(widget=forms.Textarea(attrs={'rows':'10', 'cols':'70'})) def __init__(self, user, *args, **kwargs): super(TransactionForm, self).__init__(*args, **kwargs) self.fields['account'].queryset

form.is_valid() always returning false

早过忘川 提交于 2019-12-20 04:47:13
问题 Form.is_valid() is always returning false, and it never shows any kind of error. Following is the code snippet from forms.py, views.py and template/transaction_add_page.html Form class TransactionForm(forms.Form): account = forms.ModelChoiceField(queryset=Account.objects.none()) transactions = forms.CharField(widget=forms.Textarea(attrs={'rows':'10', 'cols':'70'})) def __init__(self, user, *args, **kwargs): super(TransactionForm, self).__init__(*args, **kwargs) self.fields['account'].queryset

Too many values to unpack during template rendering

拟墨画扇 提交于 2019-12-20 04:25:05
问题 I am learning Django right now and I came across this error which I am a bit stumped on. I am trying to get my form onto the my homepage I get this error: code: home/views.py: from django.shortcuts import render from forms import TestForm from django.http import HttpResponseRedirect def home(request): if request == 'POST': # create a form instane and populate it with data from the request form = TestForm(request.POST) if form.is_valid(): # process the data in form.cleaned_data as required

Show BooleanField checkbox before label

守給你的承諾、 提交于 2019-12-20 04:16:48
问题 I'm in the process of trying to minimize the amount of code I need to use to render a form with bootstrap styling with the hope of rendering with just {{ form }} but I haven't yet managed to find a way to render a BooleanField with the checkbox before the text. from django.forms import Form, BooleanField class MyForm(Form): field = BooleanField(label='Test Label') MyForm().as_table() The above test code will output <tr><th><label for="id_field">Test Label:</label></th><td><input class="" id=

Error while running Django app

陌路散爱 提交于 2019-12-20 04:09:51
问题 When I run django project in pycharm or cmd, I get this error. What Should I do? "C:\Program Files (x86)\JetBrains\PyCharm 3.4.1\bin\runnerw.exe" C:\Python34\python.exe C:/Users/admin/PycharmProjects/untitled/manage.py runserver 8000 Validating models... 0 errors found August 15, 2014 - 13:58:27 Django version 1.6.5, using settings 'untitled.settings' Starting development server at http://127.0.0.1:8000/ Quit the server with CTRL-BREAK. Unhandled exception in thread started by <function check

Django: Get the form name or id from template

醉酒当歌 提交于 2019-12-20 04:09:31
问题 If you have multiple forms from django import forms class NameForm(forms.Form): your_name = forms.CharField(label='Your name', max_length=100) class SecondNameForm(forms.Form): your_name = forms.CharField(label='Your name', max_length=100) is there a way that, in your template, you can figure out if form in inside context belongs to NameForm or SecondNameForm I have a custom widget that in its html, it uses the id="" identifier (which should be unique in the whole html). I want to assign the