Only displaying one radio button widget from a Django RadioSelect - python

I am making a form with Django, and need to manually lay out the form in the HTML. I have one field that's a ChoiceField and the widget is a RadioSelect (i.e. radio buttons rather than select drop down). There are 2 choices in this field (i.e. there will be 2 radio buttons when it's displayed).
If i do {{ form.fieldname }}, then it puts in both radio buttons there. I want to control where each radio button goes (since I need to put other things 'beside' each radio button, rather than just the textual value from the choices parameter).
I can manually enter the correct html <input type="radio" name="fieldname"…, however this won't reflect the value, if the field is set to the first choice then this HTML won't have selected="selected" on it.
Is there anyway, in the django template, to just put in the first radio button for a field? Something like this:
{{ form.fieldname.radio_button_0 }} (some of my custom html here)
Some more stuff {{ form.fieldname.radio_button_1 }} some more html here.

This page has information on how to iterate over each radio button:
https://docs.djangoproject.com/en/dev/ref/forms/widgets/
See "class RadioSelect".

Try form.fieldname.widget.renderer[%CHOICE%]. For example if you have choices = (('o', 'one'),('t', 'two')), then valid %CHOICE% would be 'o' or 't'.

Related

show dynamic data from form value change in template

I'm new to django, i'm used to angular. i'm trying to do something that make sense to me in angular and I can't achieve in django.
I'm working with python 3.9 and django 4.1
I simplified my case to this..
I have a form that I created and and a view for it, i have a select element, whenever i select something, i want to show what i selected.
so I created a LocationForm form class:
class LocationForm(forms.Form):
apartment_type = forms.ModelChoiceField(queryset=ApartmentType.objects.all())
apartment type is just a list of apartment types (building, apartment, garden and so on)
i past the form to the view:
def location(request):
context = {'form': LocationForm}
return render(request, 'prospects/location.html', context)
and the code for the view:
{% load static %}
<form>
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Save">
</form>
CCC{{ form.apartment_type.value }}DDD<br/>
the problem is that no matter what I select, form.apartment_type.value still shows None, it's not being updated.
i guess i'm used to angular too much but this issue is really alarming for me if django only parses things statically, i guess i'm not used to it and i have no idea how to resolve it otherwise.
in my full code i want to reflect different type of items based of what chosen in the form but i can't do that if nothing gets updated.
any ideas how to resolve this?
any information regarding this issue would be greatly appreciated, i'm really lost here.
#update
it looks like i wasn't clear.
I want to understand the django template updates when variables are changed inside of it without refreshing the page.
only when i change the selection, without clicking save and sending the form i want to see the new value printed between CCC and DDD. currently i'm using {{ form.apartment_type.value }} but it stays None when i select an item.
if not.. how can i resolve this with django ?
i just said that i moved from angular and there it's like that out of the box and if here it's not, i hope there is a solution.
It seems that Django only compiles the pages when I render them first, if want to change anything in realtime I need to a javascript framework.
Found that at: https://stackoverflow.com/a/50189643/

Django template radio button auto select

I am developing an app with django. Here is the screenshot. I need to know when user will select on a plan in the next page that plan radio button should auto select.
I am able to grab all the information regarding the selected plan but dont understand how to select that plan's radio button.
here is the screenshots. Let me know anyone has the answer.
here is the next image where the radio buttons are present.
You could store the selected plan inside a sessions key and then check if that key exists in the next page... something like:
# in your first view set the session keys of posted plan
if 'express' in request.POST:
request.session['express'] = 'express chosen' # strings are easier to handle
# add the other plans to the if statement
# in the next page's view function, add keys to context if they are in the session framework like so
if 'express' in request.session:
express = request.session['express']
# add the other plans to the if statement
Now in the template, check if the variables exist to select option as default...
<input type="radio" {%if express%}checked{%endif%}>
<label>Express</label>
<!-- repeat for the other plans -->

Adding more fields to the signup form

I want to know if I can add more fields to the signup form if a user checks a box. For example, if the user checks the box that says he is a photographer one more field will appear in which he can specify what type of photography he does.
This is what DHTML is. You need to add an event on the checkbox which will dynamically add the INPUT element in the form. something like.
<input type="checkbox" onclick="addInput(this)" id="photography">
now in the method add a text element dynamically with dynamic id e.g.(value-photography) so that you can take an action on server side.
you need to make use of
document.appendChild()
You can use jQuery as well.

Update database field with multiple buttons

I'm working on a small project using Django and I have a detail page of a digital product. The user should have the avability to update the status of this product through 3 Buttons. This status represents one ChoiceField in the database. The choices are: Dismiss, In Review and Approve.
Clicking the button should submit the form directly.
My question is, what is the best way to solve that (without Ajax)?
My first idea was to add 3 forms and every form has another action url.
I'm still fairly new to Django and don't know if my approach is the best solution.
As described in this question and in the docs you should be able to get what you want with a RadioSelect form widget.
If you add a bit of CSS to hide the radio inputs themselves, and style the labels as buttons, you should be good!
Edit to include my comment below:
To get a single click submission, you could write some simple javascript that catches the onclick event on your buttons, and submits the form as you click.

How to add a pagination "go to page" form set to Django admin on a ModelAdmin form?

Here's the gist of my desired scenario:
I've got the standard Django pagination going in the admin site. What I'd like to do is let a user enter a number into a text input that corresponds to some page number. The user presses enter or clicks a button. Results for the page number entered are then shown.
Note:
I'm using Django 1.2.3 with Python 2.65
I'm modifying a ModelForm someone else created for our admin site.
Any ideas, suggestions, and/or comments greatly appreciated!
Thank you,
Michaux
Every pagination system I have come across including the standard Django pagination uses GET parameters for rendering a page.
So, providing a form to get the page number of the page to be displayed is the simplest thing you can do with html forms.
<form>
<input type="text" name="page">
<input type="submit">
</form>
To include this, you might want to override the corresponding admin template.

Categories

Resources