【发布时间】:2015-10-02 18:44:21
【问题描述】:
假设我有一个名为 addPost 的视图(就像墙上的帖子)。这是一个帖子对象的模型表单页面。有两种情况,request.method 是 post,或者不是。如果是 POST 方法,我想在提交帖子后返回个人资料页面。
我遇到过几次这个问题,它通常以 NoReverseMatch 错误的形式出现。
你如何“返回渲染/Httpresponse/etc”到另一个视图?在姜戈?我觉得我以前拥有的任何解决方案都非常老套,我想以适当的方式来实现这种功能。
我确实想指出我在 myapp/profile.html 上收到此错误
这是我的引用:
NoReverseMatch at /myapp/profile/1/
Reverse for 'addpost' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []
Request Method: GET
Request URL: http://127.0.0.1:8000/myapp/profile/1/
Django Version: 1.8.2
Exception Type: NoReverseMatch
Exception Value:
Reverse for 'addpost' with arguments '()' and 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
它出现在 html 中的这行代码,链接到 add_post 视图的按钮。
profile.html
{% if user == currUser %}
<a href="
{% url 'addpost' %}
" class = "btn btn-info"> <span class="glyphicon glyphicon-plus"></span></a>
{% endif %}
它在 Debug 中突出显示了这段代码:
~/myapp/views.py in profile
return render_to_response('myapp/profile.html', {'currUser': currUser}, context) ...
▼ Local vars
这是两个视图,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 a form instance and populate it with data from the 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'),
)
【问题讨论】:
-
发帖完成后重定向到新视图。
-
发帖成功后就重定向(reverse('profile', args=[request.user.pk]))。