django-forms

Define css class in django Forms

空扰寡人 提交于 2019-12-27 12:39:13
问题 Assume I have a form class SampleClass(forms.Form): name = forms.CharField(max_length=30) age = forms.IntegerField() django_hacker = forms.BooleanField(required=False) Is there a way for me to define css classes on each field such that I can then use jQuery based on class in my rendered page? I was hoping not to have to manually build the form. 回答1: Yet another solution that doesn't require changes in python code and so is better for designers and one-off presentational changes: django-widget

Django ModelChoiceField: filtering query set and setting default value as an object

一笑奈何 提交于 2019-12-27 11:54:40
问题 I have a Django Form class defined likes this in Models : class AccountDetailsForm(forms.Form): ... adminuser = forms.ModelChoiceField(queryset=User.objects.all()) This works OK, but it has some limitations I can't seem to work around: (1) I would like to use a filter on the queryset, based on a variable accountid passed to the form, like this: User.objects.filter(account=accountid) This can't work in the model because accountid can't be passed as a variable, of course. It follows that the

Applying bootstrap styles to django forms

99封情书 提交于 2019-12-27 11:25:09
问题 I want to use bootstrap to get a decent website design, unfortunately I don't know how style the form fields. I am talking about this: <form class="form-horizontal" method="POST" action="."> {% csrf_token %} {{ form.title.label }} {{ form.title }} </form> How is one supposed to design this?? I tried this: <form class="form-horizontal" method="POST" action="."> {% csrf_token %} <div class="form-control"> {{ form.title.label }} {{ form.title }} </div> </form> This obviously didn't gave me the

dynamically add field to a form

笑着哭i 提交于 2019-12-27 10:31:09
问题 I have 3 fields in my form. I have a submit button and a button to "Add additional Field". I understand I can add fields using __init__ method in the form class. I am new to Python and Django and am stuck with a beginner question; my question is: When I click the "Add additional field" button, what is the process to add the additional field? Does the form have to be rendered again? How and when do I call __init__ or do I even have to call it? How do I pass arguments to __init__ ? 回答1: Your

Django Replace choices value in ModelForm Widget

青春壹個敷衍的年華 提交于 2019-12-25 18:31:07
问题 i have a model that let me to choice between some predefined values. in models.py class Customer(models.Model): GENDER = ( ('m' , 'Male') , ('f' , 'Female') ) PersonGender = models.CharField(max_length=20,choices=GENDER) and i have a ModelForm to handle my form , it's ok , but i want to define another ModelForm To Change "GENDER" values for some reasons. i define another ModelForm in forms.py GENDER = (('male' , 'Male' ), ('female' , 'Female') ) class MySecondaryForm(forms.ModelForm): class

Set a models user field to the current logged in user before saving its django ModelForm

主宰稳场 提交于 2019-12-25 12:45:14
问题 i have a model form as so below class JobForm(ModelForm): class Meta: model = Job exclude = ('date_added', 'date_modified','owner','status','tags','slug','winning_tech','completiondate') The owner field is a foreignKey linked to the Django User model and it's excluded from being rendered in the form. I am trying to set the owner field to the current logged in user before i save the form. My save function is contained in the following code. def createJob(request): bix_user=getBixUser(request

Django: Redirect Change Password URL

北城以北 提交于 2019-12-25 10:57:28
问题 I have the following two URLs: url(r'^change-password/$',django.contrib.auth.views.password_change,{'template_name': 'meta/changepassword.html', 'post_change_redirect': '/password-changed/'},name='change_password'), url(r'^change-passwordiOS/$',django.contrib.auth.views.password_change,{'template_name': 'meta/changepassword.html', 'post_change_redirect': '/password-changed/'},name='change_passwordiOS'), I thought if I used the following within the change password form it would override what

Setting model user to current user when using inlineformset_factory in Django

徘徊边缘 提交于 2019-12-25 09:26:22
问题 I've hit a roadblock that I've spent hours trying to overcome, and I would appreciate some guidance. I have two models: Listings and Addresses, with the following structure: class Listings(models.Model): created_date = models.DateTimeField(auto_now_add=True) pub_date = models.DateTimeField(auto_now_add=True) address = models.ForeignKey(Addresses) user = models.ForeignKey(User, on_delete=models.CASCADE) is_active = models.BooleanField(default=1) class Addresses(models.Model): address1 = models

Django form submit with ajax

房东的猫 提交于 2019-12-25 09:15:39
问题 I have new_registration view as follows : def new_registration(request): context = {} if request.method == "POST": form = RegistrationForm(request.POST) if form.is_valid(): language = Language.objects.get(key="EN") country = Country.objects.get(key="BE") user = User() user.username = form.cleaned_data['email'] user.first_name = form.cleaned_data['first_name'] user.last_name = form.cleaned_data['last_name'] user.email = form.cleaned_data['email'] user.set_password(form.cleaned_data['password']

Django form doesn't render

回眸只為那壹抹淺笑 提交于 2019-12-25 08:49:22
问题 Hello and thank you in advance. I have a django form that is not rendering on a template. The "submit" button renders, but the form doesn't. I have been staring at this code for about 6 hours and it is probably something very simple, I probably need another pair of eyes on it. My Model: #models.py from django.db import models from django.forms import ModelForm class DraftInput(models.Model): player_id = models.IntegerField(help_text = 'Input Player ID Number', max_length = 5) def __unicode__