Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
223 views
in Technique[技术] by (71.8m points)

python - render_to_response or redirect changes the template elements in Django 1.8

I'm trying to check if email id entered by user is existing in the database table, if existing - I would like to route to 'prof.html' template otherwise just show a message in the login.html template.

Both the conditions are working fine.

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

Can we also send the context information to destination template ? (response data or any object from the database and redirect to prof.html template via view 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'),

Just for your info, 'profile' view does return some objects from the table and renders in the template app/prof.html.

I observed that the destination template is being rendered in same login.html template (How ? : In the browser url, I dont see myprofile - but the one for login) But when I request the myprofile manually by entering in the website url (localhost:xxxxx/myprofile), it works perfectly :(

URL before submitting request in login.html :

enter image description here

URL after submitting request in login.html - myprofile is rendered in the same page :

enter image description here

When I manually type in the url, template just works perfectly.. enter image description here

Could you please let me know what could be the problem ?

EDIT: Solved this issue with a little trick, posted in the below

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

1) Actually there are many ways to pass data to next view ... generally in such cases like you have better way - using sessions (cookie|localstorage|sessionstorage), it is like clipboard ... save session data in one view and get it later in another one. 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 views:

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

But if you planning just use this data in template Django has some kind more high-level interface for it and in your case semantically right to use it - The messages framework https://docs.djangoproject.com/en/1.8/ref/contrib/messages/

2) If you want redirect to another view better use url namespaces 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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...