Django - 在一个视图中,我如何"返回render / Httpresponse / etc"另一种观点?

时间:2015-07-14 08:19:15

标签: python django

所以说我有一个名为addPost的视图(就像一个墙贴)。它是一个帖子对象的模型表单页面。有两种情况,要么是request.method是post,要么是不是。如果它是POST方法,我想在提交帖子后返回个人资料页面。

我已经有过几次这个问题了,它通常以NoReverseMatch错误的形式出现。

你如何"返回渲染/ Httpresponse / etc"另一种观点?在Django?我觉得我之前遇到过的任何解决方案都是真正的hackish,我想以正确的方式实现这种功能。

我确实想要注意,我在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'),

)

2 个答案:

答案 0 :(得分:1)

只需重定向,您就不需要render_to_response

from django.shortcuts import redirect
from django.core.urlresolvers import reverse

@login_required
def add_post(request):
    ....
    if form.is_valid():
        ....
        return redirect(reverse('profile', args=[request.user.pk]))

答案 1 :(得分:1)

你误解了这个问题。它与重定向无关;它出现在模板本身中,因为您使用的是%rsi,-0x20(%rbp)而不是{% url 'addpost' %}