使用 {% url ???%} 在 Django 模板中 [英] Using {% url ??? %} in django templates

查看:24
本文介绍了使用 {% url ???%} 在 Django 模板中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在谷歌上搜索了很多关于如何在模板中使用url"标签的答案,结果发现很多回复说你只需将它插入你的模板并将其指向你想要的 url 的视图".好吧,这对我来说并不快乐:(我已经尝试了所有可能的排列方式,最后只能在此处发帖.

I have looked a lot on google for answers of how to use the 'url' tag in templates only to find many responses saying 'You just insert it into your template and point it at the view you want the url for'. Well no joy for me :( I have tried every permutation possible and have resorted to posting here as a last resort.

原来是这样.我的 urls.py 看起来像这样:

So here it is. My urls.py looks like this:

from django.conf.urls.defaults import *
from login.views import *
from mainapp.views import *
import settings

# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    # Example:
    # (r'^weclaim/', include('weclaim.foo.urls')),
    (r'^login/', login_view),
    (r'^logout/', logout_view),
    ('^$', main_view),

    # Uncomment the admin/doc line below and add 'django.contrib.admindocs' 
    # to INSTALLED_APPS to enable admin documentation:
    # (r'^admin/doc/', include('django.contrib.admindocs.urls')),

    # Uncomment the next line to enable the admin:
    (r'^admin/', include(admin.site.urls)),
    #(r'^static/(?P<path>.*)$', 'django.views.static.serve',{'document_root': '/home/arthur/Software/django/weclaim/templates/static'}),
    (r'^static/(?P<path>.*)$', 'django.views.static.serve',{'document_root': settings.MEDIA_ROOT}),
)

我的登录"目录中的views.py"如下所示:

My 'views.py' in my 'login' directory looks like:

from django.shortcuts import render_to_response, redirect
from django.template import RequestContext
from django.contrib import auth

def login_view(request):
    if request.method == 'POST':
        uname = request.POST.get('username', '')
        psword = request.POST.get('password', '')
        user = auth.authenticate(username=uname, password=psword)
        # if the user logs in and is active
        if user is not None and user.is_active:
            auth.login(request, user)
            return render_to_response('main/main.html', {}, context_instance=RequestContext(request))
            #return redirect(main_view)
        else:
            return render_to_response('loginpage.html', {'box_width': '402', 'login_failed': '1',}, context_instance=RequestContext(request))
    else:
        return render_to_response('loginpage.html', {'box_width': '400',}, context_instance=RequestContext(request))

def logout_view(request):
    auth.logout(request)
    return render_to_response('loginpage.html', {'box_width': '402', 'logged_out': '1',}, context_instance=RequestContext(request))

最后是 login_view 指向的 main.html:

and finally the main.html to which the login_view points looks like:

<html>
<body>
test! <a href="{% url logout_view %}">logout</a>
</body>
</html>

那为什么我每次都得到NoReverseMatch"?

So why do I get 'NoReverseMatch' every time?

*(稍微不同一点,我必须在所有渲染到响应的末尾使用context_instance=RequestContext(request)",否则它无法识别模板中的 {{ MEDIA_URL }} 并且我无法'不引用任何 css 或 js 文件.我不确定这是为什么.对我来说似乎不正确)*

*(on a slightly different note I had to use 'context_instance=RequestContext(request)' at the end of all my render-to-response's because otherwise it would not recognise {{ MEDIA_URL }} in my templates and I couldn't reference any css or js files. I'm not to sure why this is. Doesn't seem right to me)*

推荐答案

您应该在 urls.py 文件中提供一个字符串,而不是导入 logout_view 函数:

Instead of importing the logout_view function, you should provide a string in your urls.py file:

所以不是(r'^login/', login_view),

但是(r'^login/', 'login.views.login_view'),

这是做事的标准方式.然后,您可以使用以下方法访问模板中的 URL:

That is the standard way of doing things. Then you can access the URL in your templates using:

{% url login.views.login_view %}

这篇关于使用 {% url ???%} 在 Django 模板中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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