I just simple want to pass the emailadress from def send_username to the second def username_is_send. How do I do it? How can I pass the variable to the next def?
#csrf_protect
def send_username(request, template_name='auth/user/registration/send_username_form.html',
email_template_name='auth/user/registration/send_username_email.html',
send_username_form=SendUsernameForm, post_reset_redirect=None):
if post_reset_redirect is None:
post_reset_redirect = reverse('auth.user.registration.views.username_is_send')
if request.method == "POST":
form = send_username_form(request.POST)
if form.is_valid():
opts = {}
opts['use_https'] = request.is_secure()
opts['email_template_name'] = email_template_name
opts['request'] = request
form.send_mail_now(**opts)
return HttpResponseRedirect(post_reset_redirect)
else:
form = send_username_form()
return render_to_response(template_name, {
'form': form,},
context_instance=RequestContext(request))
def username_is_send(request, template_name='tmp/username.html'):
return render_to_response(template_name, context_instance=RequestContext(request))
Thanks!
Craphunter
You need to store the state somehow in order to pass parameters through a redirect. Multiple possibilities:
Store the mail address in the session, then read in the session variable again in the username_is_send view.
Use a GET parameter to pass the mail address.
And it's "pass", not "path". And the "def" is called (view) function.
Related
I have created a function called a panel.
When i run the server it gives an error " UnboundLocalError at /service-panel/ local variable 'username' referenced before assignment" .
I don't know what is getting wrong.
Here is views.py
#login_required
def panel(request):
if(request.session.has_key('username')):
username = request.session['username']
data = Enquiry.objects.filter(service=request.session['service'])
return render(request, 'service-provider-panel/base.html', {'username':username, 'data':data})
You have a logical error:
#login_required
def panel(request):
if(request.session.has_key('username')):
username = request.session['username']
data = Enquiry.objects.filter(service=request.session['service'])
else:
# else statement is needed because the variables username and data must be defined.
username = None
data = None
return render(request, 'service-provider-panel/base.html', {'username':username, 'data':data})
The problem is that the condition request.session.has_key('username') can be False. Then, the username variable will not be assigned. The same with data variable.
A solution could be:
#login_required
def panel(request):
username = None
data = None
if(request.session.has_key('username')):
username = request.session['username']
data = Enquiry.objects.filter(service=request.session['service'])
return render(request, 'service-provider-panel/base.html', {'username':username, 'data':data})
You will need to control the logic in the template (service-provider-panel/base.html) when data or username is None.
I'm trying to create a confirmation page. User can create orders on one page, then the create_order view validates the forms and send a request with context to another view which is called confirm_order. I think that I would work correct but there is one problem. The first time confirm_order gets request and context which contains data from forms. But when User clicks on confirm in this page, the confirm_view is called without this context so I'm getting error:
> ValidationError at /create-job/ [u'ManagementForm data is missing or
> has been tampered with']
Do you guys know how to send the context second time?
Here are those two views:
def create_order(request):
LanguageLevelFormSet = formset_factory(LanguageLevelForm, extra=5, max_num=5)
language_level_formset = LanguageLevelFormSet(request.POST or None)
job_creation_form = JobCreationForm(request.POST or None, request.FILES or None)
context = {'job_creation_form': job_creation_form,
'formset': language_level_formset}
if request.method == 'POST':
if job_creation_form.is_valid() and language_level_formset.is_valid():
cleaned_data_job_creation_form = job_creation_form.cleaned_data
cleaned_data_language_level_formset = language_level_formset.cleaned_data
context = {
'cleaned_data_job_creation_form': cleaned_data_job_creation_form,
"cleaned_data_language_level_formset": cleaned_data_language_level_formset,
}
mutable = request.POST._mutable # I'm adding parameter 'review' to be able to differ between two different posts in confirm_order view
request.POST._mutable = True
request.POST['review'] = True
request.POST._mutable = mutable
return confirm_order(request, context)
else:
return render(request, 'auth/jobs/create-job.html', context=context)
return render(request, 'auth/jobs/create-job.html', context=context)
def confirm_order(request, context):
print context
cleaned_data_job_creation_form = context['cleaned_data_job_creation_form']
cleaned_data_language_level_formset = context['cleaned_data_language_level_formset']
print request.POST['review']
if request.method == 'POST' and request.POST['review'] == True:
file = cleaned_data_job_creation_form['file']
count = 5 #simplified multiple rows
jobs = []
for language_level_form in [d for d in cleaned_data_language_level_formset if d]:
language = language_level_form['language']
level = language_level_form['level']
d = {}
d['language_from'] = cleaned_data_job_creation_form['language_from'].name
d['language_to'] = language
d['number_of_characters'] = count
d['price_per_sign'] = 1
d['estimated_price'] = count * d['price_per_sign']
jobs.append(d)
table = CreatedOrdersTable(jobs)
context = {'table': table,
'cleaned_data_job_creation_form': cleaned_data_job_creation_form,
'cleaned_data_language_level_formset': cleaned_data_language_level_formset}
return render(request, 'auth/jobs/confirm-order.html', context=context)
else:
for language_level_form in [d for d in cleaned_data_language_level_formset if d]:
language = language_level_form['language']
level = language_level_form['level']
Job.objects.create(
customer=request.user,
text_to_translate=cleaned_data_job_creation_form['text_to_translate'],
file=cleaned_data_job_creation_form['file'],
short_description=cleaned_data_job_creation_form['short_description'],
notes=cleaned_data_job_creation_form['notes'],
language_from=cleaned_data_job_creation_form['language_from'],
language_to=language,
level=level,
)
return HttpResponseRedirect('/order-success')
You are returning a template render on your first view. So, the request doesn't end on your confirm_order view.
If you want, you can redirect to reverse('confirm_order') and add the data on session.
In your confirm_order view you will pop the data from session and continue with your flow.
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())
please help to write the test.
to address:
/edit_records/38/
uses the view edit_records().
Here's a test for it:
def test_edit_records(self):
resolver = resolve('/edit_records/38/')
self.assertEqual(resolver.func, edit_records)
And for an address:
/edit_records/38/
POST={
q:1,
w:2
}
view should be used my_records()
here's a non-working test for it:
def test_edit_records_POST(self):
resolver = resolve('/edit_records/38/', {q:1, w:2})
self.assertEqual(resolver.func, my_records)
here's an view, if you want to:
def edit_records(request, id_record):
entry = Diary.get_entry(id_record=id_record, user_id=request.user.pk)
form = addMessageForm(instance=entry)
if request.method == 'POST':
form = addMessageForm(request.POST, request.FILES, instance=entry)
if form.is_valid():
form.save()
return HttpResponseRedirect('/my_records/')
t = loader.get_template('page_edit_records.html')
c = RequestContext(request, {
'form': form,
})
return HttpResponse(t.render(c))
I'm not quite sure why you're doing all that stuff with resolver. That's not really relevant to a unit test; you don't ever call the view itself, so you're not testing any of its actual functionality, the only thing you're testing here is Django's URL resolver.
Instead, you should use the functionality provided by the test client to actually call your views:
def test_edit_records(self):
response = self.client.get('/edit_records/38/')
self.assertContains(response, 'some data you expect to see in the response')
And you can do the same with the POST:
def test_edit_records_POST(self):
response = self.client.POST('/edit_records/38/', {q:1, w:2})
self.assertTrue(something_about_the_response)
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)