django-forms

Django - change select items display

牧云@^-^@ 提交于 2019-12-17 21:23:07
问题 I use ModelForm . One of the fields is: repertoire = models.ForeignKey(Repertoire) I need to change its display type. Instead of using __str__ (or __unicode__ in Python 2) in display I want to show name and date of repertoire. How can I do this with ModelForm ? 回答1: Subclass ModelChoiceField and override label_from_instance to return the repertoire name and date. Then use the new field in your ModelForm . from django import forms class RepertoireModelChoiceField(forms.ModelChoiceField): def

How to pass extra_context when redirecting in Django

为君一笑 提交于 2019-12-17 20:42:35
问题 My views.py: @login_required def some_views(request): if request.method == 'POST': form = AddressCreateFrom(request.POST) if form.is_valid(): name = form.cleaned_data['Address'] ip_value = form.cleaned_data['value'] user_list = get_username(name) address_create = form.save() extra_context = { 'user_list': user_list } return redirect_to(request, url=address_create.get_absolute_url()) else: form = AddressCreateFrom() extra_context = { 'form':AddressCreateFrom(initial={'user': request.user.pk})

Migration clashes with forms.py

♀尐吖头ヾ 提交于 2019-12-17 20:11:36
问题 The command python manage.py makemigrations fails most of time due to the forms.py , in which new models or new fields are referenced at class definition level. So I have to comment each such definitions for the migration to operate. It's a painfull task. I don't understand why the migration process import the forms.py module. I think that importing models modules should be sufficient. Is there a way to avoid those errors ? 回答1: I was having this same issue and found the specific problem.

Django: Access request object from admin's form.clean()

≯℡__Kan透↙ 提交于 2019-12-17 18:38:04
问题 My question is very similar to this one: How do I access the request object or any other variable in a form's clean() method? Except, I have the same problem with admin form. So I can't see a way to init the form myself, therefore - to pass a request to it. Thanks beforehand. 回答1: Indeed, there is a way to solve your issue! You will need to subclass form provided by ModelAdmin.get_form() and override it: class BusinessDocumentCommentForm(forms.ModelForm): def __init__(self, *args, **kwargs):

Django ModelAdmin - fieldsets … field 'date' missing from the form

[亡魂溺海] 提交于 2019-12-17 18:28:13
问题 I figured out what the problem was while writing this question. I post it anyway in case it could help someone else. The error: 'FooAdmin.fieldsets[0][1]['fields']' refers to field 'date' that is missing from the form. With the following code: # models.py from django.db import models class Foo(Base): date = models.DateField(auto_now_add=True) title = models.CharField(max_length=255) # admin.py from django.contrib import admin class FooAdmin(BaseAdmin): list_display = ("title", "date")

Implementing Ajax requests / response with django-allauth

末鹿安然 提交于 2019-12-17 18:25:06
问题 I am using django-allauth for one of my project. I would like to implement login/signup process via ajax. I would like to have customized signup form. I was going through their signupmixin and signup form. Sounds like I can write custom views for each action and map it to the url config. I am not sure what is the best way to do this. Thank you so much for any help or advice on this. 回答1: It depends a bit on what you mean by ajax. If you just want to have a popup-style login/signup box on

Django - How to prepopulate admin form fields

大兔子大兔子 提交于 2019-12-17 18:21:30
问题 I know that you can prepopulate admin form fields based on other fields. For example, I have a slug field that is automatically populated based on the title field. However, I would also like to make other automatic prepopulations based on the date. For example, I have an URL field, and I want it to automatically be set to http://example.com/20090209.mp3 where 20090209 is YYYYMMDD. I would also like to have a text field that automatically starts with something like "Hello my name is author"

Difference between Django Form 'initial' and 'bound data'?

☆樱花仙子☆ 提交于 2019-12-17 17:54:04
问题 Given an example like this: class MyForm(forms.Form): name = forms.CharField() I'm trying to grasp what the difference between the following two snippets is: "Bound Data" style: my_form = MyForm({'name': request.user.first_name}) "Initial data" style: my_form = MyForm(initial={'name': request.user.first_name}) The documentation seems to suggest than "initial is for dynamic initial values", and yet being able to pass "bound data" to the constructor accomplishes exactly the same thing. I've

Render a CheckBoxSelectMultiple form using the data present in Database. [initial value is a queryset from DB]

回眸只為那壹抹淺笑 提交于 2019-12-17 17:16:08
问题 I have a form called DemoForm which is related to model Demo class Demo(models.Model): user = models.ForeignKey(User, on_delete=models.CASCADE) ans = models.CharField(max_length=1024) and the form for this is class DemoForm(forms.ModelForm): class Meta: model = Demo exclude = ('user',) widgets = {'ans': forms.CheckboxSelectMultiple} I want to render this form using a queryset I have tried different approaches like form = DemoForm(initial=Love.objects.filter(user=request.user)) <form=GoodForm(

CSRF Token missing or incorrect

拥有回忆 提交于 2019-12-17 16:33:36
问题 Beginner at Django here, I've been trying to fix this for a long time now. I do have 'django.middleware.csrf.CsrfViewMiddleware' in my middleware classes and I do have the token in my post form. Heres my code, what am I doing wrong? from django.contrib.auth.forms import UserCreationForm from django.shortcuts import render_to_response from django.http import HttpResponseRedirect from chartsey.authentication.forms import RegistrationForm from django.template import RequestContext from django