django-forms

Why doesn't Django enforce my unique_together constraint as a form.ValidationError instead of throwing an exception?

元气小坏坏 提交于 2019-12-23 07:58:09
问题 Edit: While this post is a duplicate of Django's ModelForm unique_together validation, the accepted answer here of removing the 'exclude' from the ModelForm is a much cleaner solution than the accepted answer in the other question. This is a follow-up to this question. If I don't explicitly check the unique_together constraint in the clean_title() function, django throws an exception: IntegrityError at /journal/journal/4 duplicate key value violates unique constraint "journal_journal_owner_id

Read-Only Field in Django Form

為{幸葍}努か 提交于 2019-12-23 06:53:25
问题 How do I set a field to read-only in a Django form? I know how to disable a field but that's not what I'm looking for. Any help would be appreciated. 回答1: You can use the optional attrs parameter when defining the Field . To wit: somefield = forms.CharField( widget=forms.TextInput(attrs={'readonly':'readonly'}) ) 回答2: In django 1.9 in a Field.disabled attribute available : https://docs.djangoproject.com/en/1.9/ref/forms/fields/#disabled The disabled boolean argument, when set to True,

Django form does not appear

爱⌒轻易说出口 提交于 2019-12-23 05:35:09
问题 I'm working with building a webpage with a form using Django's Form class. I've included some sample code snippets but my problem is that I've followed the Django docs and the tutorial in Djangobook but can't get my Form to actually display on the page. forms.py from django import forms class ResearchForm(forms.Form): fam_choices = ... spec_choices = ... family = forms.ChoiceField(choices=fam_choices, required=True) species = forms.ChoiceField(choices=spec_choices, required=True) views.py

Django: saving the Form does not work (The ModelForm Filters the ForeignKey choices by request.user)

六月ゝ 毕业季﹏ 提交于 2019-12-23 05:22:57
问题 I Managed to Filter the ForeignKey choices "Context.name" by "request.user" with the Code Below. First, I defined the Models. models.py class Extension(models.Model): username = models.CharField(primary_key=True, max_length=200, help_text='') callerid = models.CharField(max_length=200, help_text='') extension = models.CharField(max_length=3, help_text='') firstname = models.CharField(max_length=200, help_text='') lastname = models.CharField(max_length=200, help_text='') password = models

Editing model ForeignKey as inline in Django administration?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-23 05:01:24
问题 Let's imagine that we have the following Django sample models: class A(models.Model): title = model.CharField(max_length=64) b = models.ForeignKey(B, blank=True, null=True) class B(models.Model): name = models.CharField(max_length=64) age = models.IntegerField() In the Django administration, field A.b is going to be represented as a dropdown widget with controls for adding new B instance, editing it and deleting. I would like to show the B model similar to the way inlines are shown. However,

How to store the name of user logged in?

无人久伴 提交于 2019-12-23 04:21:58
问题 I want to store the name of the user who is currently logged into Django in a custom form. In the admin we can do so by writing modified_by=request.user.username, but how do I do this in my own form? 回答1: You can either define the __init__ method of your Form to accept a "request" parameter, and then pass that in from your view, or you can use the thread-locals hack. 来源: https://stackoverflow.com/questions/939157/how-to-store-the-name-of-user-logged-in

Django: How can I use get_model in my use case to correctly import a class and avoid conflicts

蓝咒 提交于 2019-12-23 04:18:25
问题 I have a model with a get_form method. # models.py from model_utils.managers import InheritanceManager from breathingactivities.forms import ParentRecordForm, ChildRecordForm class ParentRecord(models.Model): .... objects = InheritanceManager() def get_fields(self, exclude=('user')): fields = self._meta.fields return [(f.verbose_name, f.value_to_string(self)) for f in fields if not exclude.__contains__(f.name)] @classmethod def get_form(self, *args, **kwargs): return ParentRecordForm class

Django form input field styling

Deadly 提交于 2019-12-23 04:13:58
问题 I have redefined form for contact us page, It's standard django-form stuff, but I'm struggling to understand how can I write description inside <inpout> field. To be precise I want to write Name inside <input> field, so how can I do this. I define <input> field like {{ form.name }} , so how can I define it more like <input type="name" class="form-control" id="id_name" placeholder="Your Name"> . <div class="col-xs-12 col-sm-12 col-md-4 col-md-offset-2 col-lg-4"> {% if form.errors %} <p style=

django ForeignKey model filter in admin-area?

风格不统一 提交于 2019-12-23 03:51:34
问题 Hi I need really very very simple example. First my models: #This my student models from django.db import models SEX_CHOICES= ( ('M', 'Male'), ('F', 'Female'), ) class Students(models.Model): student_name = models.CharField(max_length=50) student_sex = models.CharField(max_length=8, choices=SEX_CHOICES) student_city = models.Charfield(max_length=50) student_bio = models.TextField() def __unicode__(self): return self.student_name O.K. Let see my ClassRooms Model. #This my ClassRooms models

Storing user's avatar upon registration

萝らか妹 提交于 2019-12-23 03:17:27
问题 I have an extended UserProfile for registering new users. My user_created function connects to signals sent upon registering basic User instance and creates new UserProfile with extended fields from my form. Here's the code : from registration.signals import user_registered from accounts.forms import ExtendedRegistrationForm import accounts from accounts.models import UserProfile def user_created(sender, user, request, **kwargs): form = ExtendedRegistrationForm(request.POST, request.FILES)