How Django's url template tag works?

2019-06-01 19:38发布

How does {"% url 'news_id' %"} work? I do have a url pattern url(r'^((?:\w|-)+)/$', 'comments.views.home', name='news_id') but I still get NoReverseMatch.

It says

Reverse for 'news_id' with arguments '()' and keyword arguments '{}' not found. 1 pattern(s) tried: ['((?:\w|-)+)/$']

views.py

from django.shortcuts import render
from comments.models import User
from django.http import HttpResponseRedirect, HttpResponse
from django.template import RequestContext, loader, Context
from comments.models import News, User, Comment
from django.core.urlresolvers import resolve 

def home(request, url_arg):
    print "in Views.py", "and url_arg = ", url_arg
    c = Comment.objects.all().filter(news__news_id=url_arg)
    n = News.objects.all().get(news_id=url_arg)
    cont = Context({'news': n.text, 'cts': c, 'news_id': n.news_id})
    rc = RequestContext(request, cont)
    t = loader.get_template('home.html')
    print 'n = ', n
    return HttpResponse(t.render(rc))
    return render(request, 'home.html', context_dict, context_instance=RequestContext(request))

def submit(request):
    print "request.path_info = ", request.path_info
    print "in submit method and url = ", resolve(request.path_info).url_name, " & other try = ", request.resolver_match.url_name 
    news_id = request.POST.get('news_id')
    user_id = request.POST.get('user_id')
    comment_text = request.POST.get('comment')
    print "news_id =", news_id, "user_id = ", user_id, "comment_text = ", comment_text
    n = News(news_id=news_id)
    n.save()
    u = User(name='random',user_id=user_id)
    u.save()
    c = Comment(news=n, user=u, text=comment_text)
    c.save()
    return HttpResponse("Thanks!")

urls.py

from django.conf.urls import include, url
from django.contrib import admin

urlpatterns = [
    # Examples:
    url(r'^(?:\w|-)+/submit/$','comments.views.submit'),
    url(r'^((?:\w|-)+)/$', 'comments.views.home', name='news_id'),
    # url(r'^blog/', include('blog.urls')),
    url(r'^admin/', include(admin.site.urls)),
]

home.html

 <p>
    News: {{news}}<br>
    </p>
    <form action="{% url news_id %}" method="post"> {% csrf_token %}
    User Id: <input type="text" name="user_id"> <br>
    Comment:<br>
    <input type="text" name="comment" placeholder="Express your opinion ...">
    <input type="submit" value="Submit"> <br>

    {% for ct in cts %}
    {{ ct.text }}<br>
    {% endfor %}

</form>

1条回答
对你真心纯属浪费
2楼-- · 2019-06-01 20:12

You've misunderstood a few things here. In particular, you've mixed up the name of the URL pattern, and the parameters you need to pass to it.

For instance, if you wanted to capture the ID of a post in your blog and pass it to a detail view, you would normally do something like:

url(r'^(?P<post_id>)/$', views.post_detail, name='post_detail')

so the name of the pattern is post_detail and the parameter it takes is called post_id. So you would reverse it by doing:

{% url "post_detail" post_id=my_post.id %}

Also, your regex is very odd. You have a non-capturing group as the only contents of a capturing group. I can't tell what you are actually trying to do with that pattern, but normally a home page wouldn't take any parameters at all - and in fact you are not actually passing any parameters. Just use r'^$'.

查看更多
登录 后发表回答