Reputation: 605
I want to pass in the parent_username
variable in the below view function to the LevelTestModelForm
, so that I could fill in one of the form inputs as that variable automatically.
Here is the view:
def LevelTestView(request, pk):
parent_username = User.objects.get(pk=pk).username
form = LevelTestModelForm()
if request.method == "POST":
form = LevelTestModelForm(request.POST)
if form.is_valid():
form.save()
return reverse("profile-page")
context = {
"form": form
}
return render(request, "leads/leveltest.html", context)
All I want is that the user can submit this form without having to input the parent_username. I honestly don't want the field to even show up on the form, but just add it automatically when the user submits the form. I hope you guys could help. Thanks a lot.
LevelTestModelForm for additional information:
class LevelTestModelForm(forms.ModelForm):
username = parent
class Meta:
model = LevelTest
fields = (
'first_name',
'last_name',
'age',
'username',
)
Upvotes: 0
Views: 1218
Reputation: 577
I would suggest passing it as an initial
argument to the form like so:
(supposing there is a parent
field in your form)
form = LevelTestModelForm(initial={'parent': parent_username })
Upvotes: 1