Reputation: 654
I'm creating 2 forms on one template in cookiecutter-django. I have both forms working on a normal django project but when I migrated them to cookiecutter-django, the forms did not work on the user_detail template.
This is the forms.py
class NarrateForm(ModelForm):
class Meta:
model = Narrate
fields = [
'title',
'body',
]
exclude = ('status',)
class TranslateForm(ModelForm):
class Meta:
model = Translate
fields = [
'title',
'body',
]
exclude = ('status',)
These are the views.py of forms that I have:
class TranslateFormView(FormView):
form_class = TranslateForm
template_name = 'user_detail.html'
def post(self, request, *args, **kwargs):
add_translation = self.form_class(request.POST)
add_narration = NarrateForm()
if add_translation.is_valid():
add_translation.save()
return self.render_to_response(
self.get_context_data(
success=True
)
)
else:
return self.render_to_response(
self.get_context_data(
add_translation=add_translation,
)
)
class NarrateFormView(FormView):
form_class = NarrateForm
template_name = 'users/user_detail.html'
def post(self, request, *args, **kwargs):
add_narration = self.form_class(request.POST)
add_translation = TranslateForm()
if add_narration.is_valid():
add_narration.save()
return self.render_to_response(
self.get_context_data(
success=True
)
)
else:
return self.render_to_response(
self.get_context_data(
add_narration=add_narration,
)
)
Now this is the view of the user_details from cookiecutter-django
class UserDetailView(LoginRequiredMixin, DetailView):
model = User
slug_field = "username"
slug_url_kwarg = "username"
user_detail_view = UserDetailView.as_view()
This is the code on the template which works on the old django project
<form method="POST" action="#">
{% csrf_token %}
{{ add_narration }}
<button type="submit" name="submit" value="Send Narration">Submit Narration</button>
</form>
<form method="POST" action="#">
{% csrf_token %}
{{ add_translation }}
<button type="submit" name="submit" value="Send Narration">Submit Narration</button>
</form>
I've been trying to make this work for more than 2 hours already and had no luck.
Upvotes: 0
Views: 272
Reputation: 13
give the submit inputs different names as shown here
<form method="POST" action="#">
{% csrf_token %}
{{ add_narration }}
<button type="submit" name="narrationsubmit" value="Send Narration">Submit Narration</button>
</form>
<form method="POST" action="#">
{% csrf_token %}
{{ add_translation }}
<button type="submit" name="transaltionsubmit" value="Send Narration">Submit Narration</button>
</form>
Then handle them as below in your view.
def handleforms(request):
if request.method =="POST" and "narrationsubmit" in request.post:
//process
elif request.method =="POST" and "transaltionsubmit" in request.post:
//process
NB: this is for a function based view. format it for a CBV
Upvotes: 1
Reputation: 21
A possible solution could be creating a basic form (extend django.forms.Form) in the forms.py file, manually creating the fields and the modela later on in the views.py file.
Upvotes: 1