ModelForm For Registration HttpResponseError - python

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.

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()
})

UnboundLocalError when submitting form in Django App

I am trying to submit a form but receiving an error:
UnboundLocalError at /create/
local variable 'spr' referenced before assignment
Below is the section of my views.py file that is highlighted in the error, specifically:
return HttpResponseRedirect("/%i" %spr.id)
def create(response):
if response.method == "POST":
form = CreateNewSprint(response.POST)
if form.is_valid():
n = form.cleaned_data["name"]
spr = Sprint(name=n)
spr.save()
response.user.sprint.add(spr)
return HttpResponseRedirect("/%i" %spr.id)
else:
form = CreateNewSprint()
return render(response, "main/create.html", {"form": form})
I am unsure of why this is happening, any pointers in the right direction would be greatly appreciated. If any other code/information is needed please let me know.
If the form.is_valid() returns False, then it will aim to evaluate return HttpResponseRedirect("/%i" %spr.id), but spr is never set in that case. You thus should in that case rerender the invalid form:
def create(response):
if response.method == 'POST':
form = CreateNewSprint(response.POST)
if form.is_valid():
n = form.cleaned_data['name']
spr = Sprint(name=n)
spr.save()
response.user.sprint.add(spr)
return HttpResponseRedirect('/%i' %spr.id)
else:
form = CreateNewSprint()
return render(response, 'main/create.html', {'form': form})
Note: You can make use of redirect(…) [Django-doc] and
determine the url based on the view name and the parameters. This is more safe and elegant than performing string formatting and
then wrap it in a HttpResponseRedirect object [Django-doc].

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.

Why is My Django Form Executed Twice?

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())

Django -- Redirecting Views with Parameters

In the Django app I am building I would like to have the user creation process go as follows: As user signs up, if valid is then redirected to create a LIST object, and if valid is then redirected to what will be a dashboard for the LIST object just created. My views.py are as follows:
def user_signup(request):
if request.method == 'POST':
form = forms.UserSignupForm(data=request.POST)
if form.is_valid():
user = form.save()
g = Group.objects.get(name='test_group')
g.user_set.add(user)
# log user in
username = form.cleaned_data['username']
password = form.cleaned_data['password1']
user = authenticate(username=username, password=password)
login(request, user)
messages.success(request, u'Welcome to Social FollowUp')
return redirect('user_create')
else:
form = forms.UserSignupForm()
return TemplateResponse(request, 'user_signup.html', {
'form': form,
})
#login_required
#permission_required('')
def user_create(request):
if request.method == 'POST':
list_form = forms.ListForm(request.POST)
if list_form.is_valid():
list_create = list_form.save()
messages.success(request, 'List {0} created'.format(list_create.list_id))
return redirect('user_dashboard')
else:
list_form = forms.ListForm()
return TemplateResponse(request, 'dashboard/create.html', {'list_form': list_form, })
def user_dashboard(request, list_id):
try:
list_id = models.List.objects.get(pk=list_id)
except models.List.DoesNotExist:
raise Http404
return TemplateResponse(request, 'dashboard/view.html', {'list_id': list_id})
My urls.py for these views is as follows:
url(r'user/signup/$', views.user_signup, name='user_signup'),
url(r'u/dashboard/(?P<list_id>\d+)/$', views.user_dashboard, name='user_dashboard'),
url(r'u/list/create/$', views.user_create, name='user_create'),
When I try to run through the process, the first two views work correctly. However when I redirect to the user_dashboard I get the following error:
Reverse for 'user_dashboard' with arguments '' and keyword arguments '{}' not found.
which sites this:
return redirect('user_dashboard')
I'm assuming this has something to do with me not passing in a list_id, however, even when I tried to pass in a hardcoded value it did not work (like this):
return redirect('user_dashboard', {'list_id': 2})
What am I doing wrong here?
Try:
return redirect(reverse('user_dashboard', args=(2,)))
Your code
return redirect('user_dashboard')
would not work because in your url pattern, you have
url(r'u/dashboard/(?P<list_id>\d+)/$', views.user_dashboard, name='user_dashboard'),
which requires list_id as a parameter.

Categories

Resources