What I'm trying to accomplish here is how to actually do a chained dropdown when there's no authentication. I've managed to create a form using chained dropdowns but those were tied to the 'current user'.
Now my problem is that i need to do exactly the same but on a form where you don't need to authenticate and I'm not sure how to do the relationship between the dropdowns.
models.py
class UserProfile(models.Model):
user = models.OneToOneField(User)
business = models.ForeignKey(Business)
role = models.ForeignKey(Role)
telephone = models.CharField(max_length=20)
title = models.CharField(max_length=60)
forms.py
class UserProfileForm(forms.ModelForm):
class Meta:
model = UserProfile
fields = ('business', 'role', 'telephone', 'title',)
views.py
def register(request):
if request.method == 'POST':
profile_form = UserProfileForm(data=request.POST)
# If the form is valid...
if profile_form.is_valid():
profile = profile_form.save(commit=False)
profile.user = user
# Now we save the UserProfile model instance.
profile.save()
# Redirect after succesful form submssion
return HttpResponseRedirect('/home/')
else:
print (profile_form.errors)
else:
profile_form = UserProfileForm()
return render(request,
'home/register.html',
{'profile_form': profile_form, 'registered': registered})
register.html
<form>
{% csrf_token %}
<div class="form-group">
{{ profile_form | crispy }}
</div>
<input type="submit" name="submit" value="Register"/>
</form>
Now when that webpage gets rendered i do get the dropdowns for business and role(ALL of them), but i haven't been able to find how to chain what roles to display based on what business has.
Hopefully I explained myself.
thank you,