Why is My Django Form Executed Twice? - python

Can someone explain to me why form 2 executed twice? In another word, I would see 2 print statements, "Hello from form 2," in the console.
The first print statement occurred after I clicked "Submit" from form 1. Second print statement comes after the second "Submit" I clicked from form 2. How do I make it to only print once?
views.py
def form1 (request):
NameFormSet = formset_factory (NameForm, formset = BaseNodeFormSet, extra = 2, max_num = 5)
if request.method == 'POST':
name_formset = NameFormSet (request.POST, prefix = 'nameform')
if name_formset.is_valid ():
data = name_formset.cleaned_data
request.session ['data'] = data
return HttpResponseRedirect ('form2')
else:
name_formset = NameFormSet (prefix = 'nameform')
context = {'name_formset': name_formset}
return render (request, 'nameform/form1.html', context)
def form2 (request):
data = request.session ['data']
print ('Hello from form 2') # <==== This statement printed twice in the console
CheckBoxFormSet = formset_factory (CheckBox, extra = 2, max_num = 5)
if request.method == 'POST':
checkbox_formset = CheckBoxFormSet (request.POST, prefix = 'checkbox')
if checkbox_formset.is_valid ():
for i, form in enumerate (checkbox_formset.cleaned_data):
data [i].update (form) # Join cleaned data with original data
del request.session ['data']
context = {'data': data}
return render (request, 'nameform/success.html', context)
checkbox_formset = CheckBoxFormSet (prefix = 'checkbox')
context = {'checkbox_formset': checkbox_formset, 'data': data}
return render (request, 'nameform/form2', context)
Update 1:
The "print" statement is actually a backend method that processes the data obtained from form 1 and display it in form 2. Leaving where it is now would cause that method to process the information twice. I have no issue or error doing it this way but it's unnecessary.
For example:
def form2 (request):
data = request.session ['data']
n, errors = getInfo (data) # <==== This statement performed twice in the console
if request.method = 'POST':
....
if checkbox_formset.is_valid ():
for i, form in enumerate (checkbox_formset.cleaned_data):
data [i].update (form) # Join cleaned data with original data
n.process_new_data (data, errors)
del request.session ['data']
context = {'data': data, 'errors': error}
return render (request, 'nameform/success.html', context)
else:
checkbox_formset = CheckBoxFormset (prefix = 'checkbox')
context = {'data': data, 'errors': error}
return render (request, 'nameform/form2.html', context)
Update 2:
Since my explanation is a little long, allow me address Ale question here.
Yes, I fully understand why it processed twice. To briefly answer your question, putting getInfo inside 'POST' will give me a context, unbound error because of the context "errors" dictionary doesn't exist in the first redirect.
context = {'data': data, 'errors': errors}
I'd to update my post so that I can explain why I can't use your method. GetInfo takes the data from form1, processes it, and passes it on to form 2 to display. I could do all that in form1 but then I would have to redo it in form2 because form2 will not know what 'n' or 'errors' is without passing it through sessions. I'm just trying to see if there's a better way to do this.

The form2 view is run twice, once as a redirect from form1 which creates the form and renders the template, missing the if request.method == 'POST' part as this time around the request is a 'GET'.
When you submit form2 back to the same view method it prints the line you indicate again, this time the code in the if block executes as the request is a 'POST'.
The key is this line that redirects to the form2 view:
return HttpResponseRedirect ('form2')

You can debug this by including the stacktrace to you print statements:
import traceback
print ''.join(traceback.format_stack())

Related

Django form has no attribute 'cleaned_data'

This is the code I have, and when I run it on Django, I am met with this error: 'Title' object has no attribute cleaned_data
def new(request):
form = Title(request.POST)
if request.method == "POST":
if form.is_valid:
title = form.cleaned_data["title"]
text = form.cleaned_data["text"]
util.save_entry(title, text)
else:
return render(request, "encyclopedia/error.html",{
"form":NewForm()
})
return redirect(reverse('page', args = [title]))
return render(request, "encyclopedia/newpage.html",{
"form1":Title(),
"form": NewForm()
})
You are probably getting the exception thrown in your return statement where you are instantiating a new Title object. This object only gets the cleaned_data attribute when is_valid method has been called upon. Hence you haven't called this on the new Title object and that is the reason why you are getting the error.
you use form = Title(request.POST), but the line after you check whether the request.method equals POST or not. i think yo should move that line inside the if statement
is_valid() it's a function
def new(request):
form = Title(request.POST)
if request.method == "POST":
if form.is_valid():
title = form.cleaned_data["title"]
text = form.cleaned_data["text"]
util.save_entry(title, text)
else:
return render(request, "encyclopedia/error.html",{
"form":NewForm()
})
return redirect(reverse('page', args = [title]))
return render(request, "encyclopedia/newpage.html",{
"form1":Title(),
"form": NewForm()
})

Django2: After form submission is there a better way to 'wipe' the POST to stop re-submission

I have a django application and on one page I have several forms for different models. I would like a 'success' message, which is easy to do by just adding to the context after form submission/validation. However, this leaves the possibility of re-submission of the form, which would just produce an error back to the page, but it still annoys me.
urls:
url_patterns = [
re_path(r'^manager/$', Manager.as_view(), name='manager'),
.......more.....
]
views.py:
class Manager(LoginRequiredMixin, View):
template_name = 'results/manager_templates/manager.html'
form1 = Form1
form2 = Form2
login_url = '/'
redirect_field_name = 'redirect_to'
def get(self, request, *args, **kwargs):
form1 = self.form1()
form2 = self.form2()
context = {
'form1': form1,
'form2': form,}
return render(request, self.template_name, context)
def post(self, request, *args, **kwargs):
submit_clicked = request.POST.get('submit')
form1 = self.form1()
form2 = self.form2()
context = {}
if submit_clicked == 'Form 1':
form1 = self.form1(request.POST)
if form1.is_valid():
form1.save()
context['message'] = 'Form 1 successful'
# reset the form
form1 = self.form1()
# return HttpResponseRedirect(
# reverse('results:manager',
# ))
else:
print('NOT VALID')
elif submit_clicked == 'Form 2':
... do same stuff as above ...
context['form1'] = form1
context['form2'] = form2
return render(request, self.template_name, context)
If I were to uncomment out the HttpResponseRedirect out, after the form was validated and added like so:
return HttpResponseRedirect(
reverse('results:manager',
))
Then it returns me to my page, and if i refresh the form isnt re-submitted. However I can't pass this an argument without it going through the url:
i.e if I were to write:
return HttpResponseRedirect(
reverse('results:manager',
kwargs={'success':'success'}
))
I get the error:
Reverse for 'manager' with keyword arguments '{'success': 'success'}' not found. 1 pattern(s) tried: ['manager/$']
and if I change urls.py to:
url_patterns = [
re_path(r'^manager/$', Manager.as_view(), name='manager'),
re_path(r'^manager/(?P<success>)$', Manager.as_view(), name='manager'),
]
I get the error:
Reverse for 'manager' with keyword arguments '{'success': 'success'}' not found. 2 pattern(s) tried: ['manager/(?P<success>)$', 'manager/$']
Is there anyway to pass HttpResponseRedirect variables that dont need to be added to url regex? Or is there any other way to 'reset' my request.POST so that forms dont get re-submitted, without using HttpResponseRedirect?
As you've found, you should redirect after a successful post to prevent duplicate requests.
When you changed the urls, you didn't add any characters to match in your success group.
re_path(r'^manager/(?P<success>\w+)$', Manager.as_view(), name='manager'),
Another option is to store the variable in the querystring, e.g. /manager/?success=success, then you can retrieve the value from request.GET after the redirect.
You could also store data in the session, or use the messages framework.

submitting two forms one after another

I am working on my first django webaite, I am trying to submit two forms one after the other.
Here is the views.py :
def home(request):
import json
if request.method == 'POST':
form = MajorForm(request.POST)
if form.is_valid():
url = 'http://www.mysite.com:8082'
dataout = {'my':'data'}
headers = {'content-type':'application/json'}
r = requests.post(url,data=json.dumps(dataout),headers=headers)
return collector(request)
else:
return HttpResponse("thnx")
else:
form = MajorForm()
return render(request,'index.html',{'form':form})
def collector(request):
if request.method == 'POST':
form = ContactForm(request.POST)
if form.is_valid():
return HttpResponse("thanx")
else:
return HttpResponse("not valid")
else:
form = ContactForm();
return render(request,'collector.html',{'form':form})
So the first view calls the second view. The first form works fine, and the second form is also displayed fine, but submitting the second form does not work at all ( I was never able to get to form.is_valid path). Maybe this entire approach of calling one view from another is not correct? What would be the right one?
Please indent your code correctly. Also you are missing an else in the collector function when the request.method is not POST.

Filter only if the value is defined in Django

I have the following view:
def process(request):
if request.method == 'POST':
data = request.POST
results = Specs.objects.filter(screenGroup = data['screen_user'], storage = data['storage_user'], mSystem = data['system_user'] )
context = {'results' : results}
return render(request, 'process.html', context)
When the user inputs the three values it filters correctly, but when it just inputs one or two (or nothing), then it filters passing the value None. Is there any way to ignore the filter if it's not set?
Thanks!
EDIT:
The following code is working, but it's obviously a very unefficient way:
def process(request):
if request.method == 'POST':
data = request.POST
if(data['screen_user'] != None):
results = Specs.objects.filter(screenGroup = data['screen_user'])
elif (data['storage_user'] != None):
results = Specs.objects.filter(storage = data['storage_user'])
else:
results = Specs.objects.all()
#plus all the other options...
context = {'results' : results}
return render(request, 'process.html', context)
You can build the filter beforehand:
def process(request):
if request.method == 'POST':
data = request.POST
spec_filter = {}
for attribute in ['screenGroup', 'storage', 'mSystem']:
if attribute in data and data[attribute]:
spec_filter[attribute] = data[attribute]
results = Specs.objects.filter(**spec_filter)
context = {'results' : results}
return render(request, 'process.html', context)
NB: To use this verbatim you would have to change the names of the variables being passed in the request.POST to match those in the Specs model. I did this just to illustrate, but you can easily use the same principle with your variable names. In that case you'll have to be a bit more verbose.
It's called validating your form.. There are two ways of doing this:
create a django form and use myform.is_valid(). You can read about it in the docs
validate it yourself with a few 'if' statements (either on server side or with javascript before sending the ajax call)

ModelForm For Registration HttpResponseError

First off, I know what the error means, I'm just confused on the configuration.
I'm getting an error of:
views.Registration didn't return an HttpResponse object
The issue is when I visit localhost/Register, I get the above error.
Q: If I want localhost/Register to show form from RegistrationForm() when it loads the register.html template within render() (at the bottom) when /Register is accessed. How do I do that? Do I need to create another view like /NewUser that I currently have specified? My thought was that render() was going to execute to show the template (with the form inside it) when viewing /Register
Code:
a view of:
def Registration(request):
RegForm = RegistrationForm(request.POST or None)
if request.method == 'POST':
if RegForm.is_valid():
clearUserName = RegForm.cleaned_data['userNm']
clearPass = RegForm.cleaned_data['userPass']
RegForm.save()
try:
return HttpResponseRedirect('/NewUser/?user=' + clearUserName)
except:
raise ValidationError('Invalid Request', code='300') ## [ TODO ]: add a custom error page here.
else:
RegForm = RegistrationForm()
return render(request, 'VA/reuse/register.html', {
'form': RegForm
})
You need to render something if the request is 'GET' instead of 'POST': ie.
def Registration(request):
RegForm = RegistrationForm(request.POST or None)
if request.method == 'POST':
if RegForm.is_valid():
clearUserName = RegForm.cleaned_data['userNm']
clearPass = RegForm.cleaned_data['userPass']
RegForm.save()
try:
return HttpResponseRedirect('/NewUser/?user=' + clearUserName)
except:
raise ValidationError('Invalid Request', code='300') ## [ TODO ]: add a custom error page here.
else:
RegForm = RegistrationForm()
return render(request, 'VA/reuse/register.html', {
'form': RegForm
})
else:
RegForm=RegistrationForm()
return render(request, 'template.html', {'formset': RegForm})
of course, you should change the context for your template, depending on whatever it is you need to render.
No, you should just move everything from the else onwards back one indentation level. Otherwise, nothing is returned if the request is not a POST.

Categories

Resources