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

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

问题描述

我在google上看了很多关于如何在模板中使用'url'标签的答案,只能找到许多回应,说你只需将其插入到你的模板中,并将其指向你想要的URL的视图。对我来说,没有快乐(我已经尝试过每一个排列的可能性,并且已经在这里发布了最后的手段。



所以这里是我的urls.py看起来像这个来自:django.conf.urls.defaults import中的

 
从login.views导入

从mainapp.views导入*
导入设置

#取消注释接下来的两行以启用admin:
from django.contrib import admin
admin.autodiscover()

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

#取消注释下面的admin / doc行,并将'django.contrib.admindocs'
#添加到INSTALLED_APPS以启用管理员文档:
#(r'^ admin / doc /',include('django.contrib.admindocs .urls')),

#取消注释下一行以启用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。服务',{'document_root':settings.MEDIA_ROOT}),

我的'login'目录中的views.py'如下所示:

  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)
#如果用户登录并且处于活动状态
如果用户不是None和user.is_active:
auth.login(re任务,用户)
返回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:

 < HTML> 
< body>
测试! < a href ={%url logout_view%}> logout< / a>
< / body>
< / html>

那么为什么每次都会得到NoReverseMatch?



*(在稍微不同的注释,我不得不在我所有的呈现到响应结束时使用'context_instance = RequestContext(request)',因为否则它不会在我的模板中识别{{MEDIA_URL}}我不能引用任何css或js文件,我不知道为什么是这样的。对我来说似乎不对)*

解决方案

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



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



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



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

  {%url login.views.login_view%} 


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.

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}),
)

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

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>

So why do I get 'NoReverseMatch' every time?

*(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)*

解决方案

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

So not (r'^login/', login_view),

but (r'^login/', 'login.views.login_view'),

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天全站免登陆