Django的网址模板标签如何工作? [英] How Django's url template tag works?

查看:127
本文介绍了Django的网址模板标签如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何 {%urlnews_id'%} 工作?
我有一个url模式 url(r'^((?:\w | - )+)/ $','comments.views.home',name ='news_id' )但我仍然得到 NoReverseMatch

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.

它说


使用参数'()'和关键字参数'{}'找不到'news_id'。 1模式尝试:['((?:\w | - )+)/ $']

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>


推荐答案

你在这里误解了一些事情。特别是,您已经混合了URL模式的名称,需要传递给您的参数

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.

例如,如果您想要捕获博客中的帖子的ID并将其传递给详细信息,则通常会执行以下操作:

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')

所以模式的名称是 post_detail ,它所需的参数称为 post_id 。所以你可以通过这样做来反转它:

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 %}

另外,你的正则表达式很奇怪。您将非捕获组作为捕获组的唯一内容。我无法确定你正在试图对这种模式做什么,但一般来说,一个主页根本就不会有任何参数 - 实际上你并没有传递任何参数。只需使用 r'^ $'

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'^$'.

这篇关于Django的网址模板标签如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆