Django Form Dropdown List of Numbers

django form dropdown list of numbers

You're looking for a ChoiceField which renders as a select html element by default.
https://docs.djangoproject.com/en/dev/ref/forms/fields/#choicefield

class CronForm(forms.Form):
days = forms.ChoiceField(choices=[(x, x) for x in range(1, 32)])

Django Forms: How to create a simple dropdown list in a form using values from an existing model

Using values_list() with flat=True will do it.

Change:

queryset=constituency.objects.all().values('name')

To:

queryset=constituency.objects.all().values_list('name', flat=True)

django form dropdown list of stored models

You should use ModelChoiceField.

class CronForm(forms.Form):
days = forms.ModelChoiceField(queryset=Books.objects.all().order_by('name'))

Then your views, it should look something like this:

def show_book(request):
form = CronForm()
if request.method == "POST":
form = CronForm(request.POST)
if form.is_valid:
#redirect to the url where you'll process the input
return HttpResponseRedirect(...) # insert reverse or url
errors = form.errors or None # form not submitted or it has errors
return render(request, 'path/to/template.html',{
'form': form,
'errors': errors,
})

To add a new book or edit one, you should use a ModelForm. Then in that view you'll check if it's a new form or not

book_form = BookForm() # This will create a new book

or

book = get_object_or_404(Book, pk=1)
book_form = BookForm(instance=book) # this will create a form with the data filled of book with id 1

Django Model Form Integer as Drop Down

You can use a ChoiceField as

class InviteForm(forms.ModelForm):
MAX_GUESTS = (
('0', '0'),
('1', '1'),
('2', '2'),
('3', '3'),
('4', '4'),
('5', '5'),
('6', '6'),
('7', '7'),
('8', '8'),
('9', '9'),
('10', '10'),
)
max_guests = forms.ChoiceField(choices=MAX_GUESTS)
class Meta:
model = Invite
fields = ['max_guests', 'rsvp_guests', 'rsvp_attendance']

In Django how to populate a form dropdown list form a column in a database

It looks like you want to use a foreign key pointing out to the Forum table in Machina.

If that is the case, then you should define the business_location_state field as a models.ForeignKey.

This ForeignKey should be pointing out to the Forum model. If you look at Machina source code you can see how they access this model and replicate it.

from machina.core.db.models import get_model

Forum = get_model("forum", "Forum")

class CustomUser(AbstractUser):
age = models.PositiveIntegerField(null=True, blank=True)
business_location_state = models.ForeignKey(Forum, on_delete=models.SET_NULL)

Django: Set a dropdown form field to a value from the current detail view's Model

Found the answer after posting. For anyone else looking for the answer.

When creating a new form instance (in my case within the get_context_data of my detail view class) use the initial parameter:

context['change_stage_form'] = ChangeStageForm(initial={
'assessment_stage': self.object.assessment_stage
})

Django Forms Docs: https://docs.djangoproject.com/en/dev/ref/forms/api/#initial-form-values

Thank you to: https://stackoverflow.com/a/813474/16395136



Related Topics



Leave a reply



Submit