I have a model, form, view and i cannot seem to actually display the RadioSelect widget. it doesnt show anything, i think everything is fine except the views, however, i am unsure. I basically want someone to choose and option from the two radio buttons, submit to then be able to register as one of the two options but like i said i cant even get the buttons to show.
views.py
def registration(request):
reg = Registration.objects.all()
return render(request, 'HTML/Registration.html', {"reg":reg})
models.py
class Registration(models.Model):
OPTIONS = (
('COUNS','Counsellor'),
('CLIENT','Client'),
)
SELECT = models.CharField(max_length=15,
choices=OPTIONS)
forms.py
class Registration(forms.ModelForm):
class Meta:
model = Registration
fields = '__all__'
widgets = {'SELECT': forms.RadioSelect}
HTML
<form method="POST">
{% csrf_token %}
<div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3
border-bottom">
<h1 class="h2">Registration</h1>
</div>
{% render_field reg.SELECT %}
<div class="form-row">
<div class="col pt-md-2">
<input type="submit" value="Next" class="btn btn-primary" style="float: right;">
</div>
</div>
</form>
You are referring to the model, not the form. Please do not give the model and the form the same name. It is quite common to a dd a suffix Form
to the form.
# app/forms.py
from django import forms
from app.models import Registration
class RegistrationForm(forms.ModelForm):
class Meta:
model = Registration
fields = '__all__'
widgets = { 'SELECT': forms.RadioSelect }
Then in the view, you can create such form as:
# app/views.py
from app.forms import RegistrationForm
from django.shortcuts import redirect
def registration(request):
if request.method == 'POST':
reg = RegistrationForm(request.POST, request.FILES)
if reg.is_valid():
reg.save()
return redirect('name-of-some-view')
else:
reg = RegistrationForm()
return render(request, 'HTML/Registration.html', {'reg': reg})
Note: In case of a successful POST request, you should make a
redirect
[Django-doc] to implement the Post/Redirect/Get pattern [wiki]. This avoids that you make the same POST request when the user refreshes the browser.