python - Django - In one view, how do I "return render/Httpresponse/etc" to another view? -
so have view called addpost(like wall post). it's model form page post object. there 2 cases, either request.method post, or isn't. in case it's post method, want return profile page after post submitted.
i've had problem few times, , comes in form of noreversematch errors.
how "return render/httpresponse/etc" view? in django? feel solutions i've had before have been hackish , want proper way implement sort of feature.
i want note error on myapp/profile.html
here trackback have:
noreversematch @ /myapp/profile/1/ reverse 'addpost' arguments '()' , keyword arguments '{}' not found. 0 pattern(s) tried: [] request method: request url: http://127.0.0.1:8000/myapp/profile/1/ django version: 1.8.2 exception type: noreversematch exception value: reverse 'addpost' arguments '()' , keyword arguments '{}' not found. 0 pattern(s) tried: [] exception location: /library/python/2.7/site-packages/django/core/urlresolvers.py in _reverse_with_prefix, line 496 python executable: /library/frameworks/python.framework/versions/2.7/resources/python.app/contents/macos/python python version: 2.7.8 python path: ['/users/me/project', '/library/frameworks/python.framework/versions/2.7/lib/python2.7/site-packages/setuptools-18.0.1-py2.7.egg', '/library/python/2.7/site-packages/pip-1.5.6-py2.7.egg', '/library/frameworks/python.framework/versions/2.7/lib/python27.zip', '/library/frameworks/python.framework/versions/2.7/lib/python2.7', '/library/frameworks/python.framework/versions/2.7/lib/python2.7/plat-darwin', '/library/frameworks/python.framework/versions/2.7/lib/python2.7/plat-mac', '/library/frameworks/python.framework/versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages', '/library/frameworks/python.framework/versions/2.7/lib/python2.7/lib-tk', '/library/frameworks/python.framework/versions/2.7/lib/python2.7/lib-old', '/library/frameworks/python.framework/versions/2.7/lib/python2.7/lib-dynload', '/library/frameworks/python.framework/versions/2.7/lib/python2.7/site-packages', '/library/python/2.7/site-packages'] server time: tue, 14 jul 2015 08:12:13 +0000
it occurs @ line of code in html, button links add_post view.
profile.html
{% if user == curruser %} <a href=" {% url 'addpost' %} " class = "btn btn-info"> <span class="glyphicon glyphicon-plus"></span></a> {% endif %}
and highlights bit of code in debug:
~/myapp/views.py in profile return render_to_response('myapp/profile.html', {'curruser': curruser}, context) ... ▼ local vars
here 2 views, profile , add_post.
@login_required def profile(request, id): context = requestcontext(request) curruser = user.objects.get(pk = id) profile = userprofile.objects.filter(user = curruser) return render_to_response('myapp/profile.html', {'curruser': curruser}, context) @login_required @login_required def add_post(request): context = requestcontext(request) if request.method == 'post': # #create form instance , populate data request form = postform(request.post) if form.is_valid(): #process data in form.clean_data post = post(user = request.user, title =form.cleaned_data['title'], body=form.cleaned_data['body']) post.save() return redirect(reverse('myapp/profile', args=[request.user.pk])) else: form=postform() return render_to_response('myapp/addpost.html', {'form': form}, context )
urls.py
urlpatterns = patterns( '', ... url(r'^profile/(?p<id>\d+)/$', views.profile, name='profile'), url(r'^addpost/$', views.add_post, name='add_post'), )
you've misdiagnosed problem. nothing redirecting; occurring in template itself, because have used {% url 'addpost' %}
instead of {% url 'add_post' %}
.
Comments
Post a Comment