Render_to_response or redirection modifies template elements in Django 1.8

I am trying to check if the email identifier entered by the user in the database table exists, if it exists, I would like to send the template "prof.html", otherwise I will simply display the message in the login.html template.

Both conditions work fine.

However, the problem is that I use redirect () or render_to_response () - the elements of the destination template, such as div, input, etc., are automatically changed (prof.html in this case)?

Can I also send contextual information to the destination template? (response data or any object from the database and redirect to the prof.html template through the presentation in this case)

Below is my code:

Views.py

def verifyme(request): if request.method == "POST": emailid4loginV = request.POST['emailid4login_Aj'] else: emailid4loginV = '' response_data = '' return HttpResponse(response_data, content_type="text/plain") response_data = '' if Employee.objects.filter(email = emailid4loginV).exists(): response_data='Thanks for waiting - login successful' #return render_to_response('app/prof.html', { 'response_data':response_data}, # context_instance = RequestContext( request ) ) return redirect('/myprofile') else: response_data='Ouch! you are not a registered user!' return HttpResponse(response_data, content_type="text/plain") 

urls.py

 url(r'^myprofile$', 'app.views.profile', name='profile'), 

For your information only, viewing a profile allows you to return some objects from a table and display them in the app / prof.html template.

I noticed that the destination template is displayed in the same login.html template (like ?: in the browser url, I do not see myprofile - but the one that is needed to enter the system). But when I request myprofile manually by typing url into the website (localhost: xxxxx / myprofile), it works fine: (

URL before submitting a request in login.html:

enter image description here

URL after sending the request to login.html - myprofile is displayed on the same page:

enter image description here

When I manually type url, the template works fine. enter image description here

Could you tell me what could be the problem?

EDIT: Solved this problem with a little trick posted in below

https://stackoverflow.com/questions/31091938/why-is-httpresponserederedrereverse-doesnt-redirect-to-new-page

+2
python django django-templates django-views
source share
1 answer

1) In fact, there are many ways to transfer data in the following form ... as a rule, in such cases as you have the best way - using sessions (cookie | localstorage | sessionstorage), this looks like a clipboard ... saving Session data at one glance and get it later in another. For example:

First view:

 self.request.session['response_data'] = 'some text' self.request.session.set_expiry(0) # user's session cookie will expire when the user's Web browser is closed. 

Other types:

 response_data = self.request.session.get('response_data', '') 

But if you plan to just use this data in the template, Django has a better interface for it and in your case the semantically right to use it - Message structure https://docs.djangoproject.com/en/1.8/ref/contrib/messages/

2) If you want to redirect to another view, it is better to use the namespace URL and reverse https://docs.djangoproject.com/en/1.8/ref/urlresolvers/#reverse

 return HttpResponseRedirect(reverse(app.views.profile)) # here I've passed callable object because you have not show your app url namespace, but generally use namespaces 

https://docs.djangoproject.com/en/1.8/topics/http/urls/#url-namespaces

+1
source share

All Articles