【问题标题】:django {% tag %} problemdjango {% tag %} 问题
【发布时间】:2011-06-04 03:34:41
【问题描述】:

我不知道是不是我,但是 {% tag ??? %} 蜜蜂在我周围偶尔表现出一些行为(django 版本 1.2.3)。我有以下 main.html 文件:

<html>
{% include 'main/main_css.html' %}
<body>
test! <a href="{% url login.views.logout_view %}">logout</a>
test! <a href="{% url client.views.client_search_last_name_view %}">logout</a>
</body>
</html>

urls.py 是:

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

# 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.views.login_view'),
    (r'^logout/$', 'login.views.logout_view'),
    (r'^$', 'mainapp.views.main_view'),

    (r'^client/search/last_name/(A-Za-z)/$', 'client.views.client_search_last_name_view'),
    #(r'^client/search/post_code/(A-Za-z)/$', 'client.views.client_search_last_name_view'),

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

和用于登录的views.py是:

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

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 redirect(mainapp.views.main_view)
        else:
            return render_to_response('loginpage.html', {'login_failed': '1',}, context_instance=RequestContext(request))
    else:
        return render_to_response('loginpage.html', {'dave': '1',}, context_instance=RequestContext(request))

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

客户的views.py是:

from django.shortcuts import render_to_response, redirect
from django.template import RequestContext
import login.views

def client_search_last_name_view(request):
    if request.user.is_authenticated():
        return render_to_response('client/client_search_last_name.html', {}, context_instance=RequestContext(request))
    else:
        return redirect(login.views.login_view)

然而,当我登录时,django 为 {% url client.views.client_search_last_name_view %} 引发了一个“NoReverseMatch”,但不是为 {% url login.views.logout_view %} 引发了一个“NoReverseMatch”

现在为什么会这样?

【问题讨论】:

    标签: django django-views django-urls


    【解决方案1】:

    如果您正在创建一个应该接受姓氏的 URL,正确的方法如下:

    (r'^client/search/last_name/(?P<last_name>[a-zA-Z]+)/$',
        'client.views.client_search_last_name_view'),
    

    正则表达式的(?P&lt;last_name&gt;[a-zA-Z]+) 部分允许您捕获长度至少为一个字符的姓氏,然后将其作为参数传递给您的视图函数。

    但是你需要确保你的视图接受这个论点:

    def client_search_last_name_view(request, last_name):
        ...
    

    你不能这样做的原因:

    {% url client.views.client_search_last_name_view %}
    

    是因为您的正则表达式声明(如视图)它需要一个参数,该参数是一个由从 A 到 Z 的小写或大写字母组成的字符串。例如,这将起作用:

    {% url client.views.client_search_last_name_view 'somelastname' %}
    

    如果你想给你的 URL 一个 name 作为另一个答案建议你可以,但这是一个单独的问题,除了缩短该模板标记之外没有任何效果。


    {% url login.views.logout_view %} 起作用的原因是因为它在urls.py 中的条目没有指定要传递给视图的任何参数,并且仅在默认情况下您没有传递任何参数。

    【讨论】:

    • 举起手来……你说得对!正如您从其他 cmets 中看到的那样,我决定改用 POST 运行,因为我想按字母顺序列出所有客户端。我不认为正则表达式不像 POST 那样需要什么?
    • 您不应该发布,如果您建议使用 POST 使其安全,但事实并非如此。 POST 应该只用于向服务器提交数据,而不是向用户显示搜索结果页面。 GET 请求应该用于所有可见的页面。
    • 又是你的权利!在考虑如何最好地进行搜索提交(或为此提交文本输入)时,我刚开始走错路
    【解决方案2】:

    AFAIK 您想为每个 url 正则表达式添加一个 name='foo' arg。该名称是反向匹配中使用的名称。像这样:

    urls.py

    (r'^login/$', 'login.views.login_view', name="login"),
    

    模板.html

    {% url login %}
    

    【讨论】:

    • 从我在 (stackoverflow.com/questions/4599423/…) 中对惰性科学的重播中可以看出,PyCharm 要求我导入各种库以使其工作。不管我导入哪一个,我总是会收到无效的语法错误。
    【解决方案3】:

    “client.views.client_search_last_name_view”网址的正则表达式捕获一个值(带括号),因此为了{% url %} 它,您需要为该参数传递一个值。

    【讨论】:

    • 不高兴 :( 我仍然得到 >Caught NoReverseMatch while rendering: Reverse for 'client.views.client_search_last_name_view' with arguments '(1,)' and keyword arguments '{}' not found.{% url client.views.client_search_last_name_view 1 %}
    • 也许仔细检查“客户端”是否在您的 INSTALLED_APPS 中?
    • @Sevenearths:如果您的正则表达式中有(A-Za-z),则如果您将1 作为参数传递,它将不起作用,它必须是一个字母... url 模式仅在以下情况下匹配参数也匹配正则表达式!
    • 啊,对,@lazerscience 说的;传递给 URL 的值实际上需要被正则表达式捕获。
    • 亚当·范登伯格:好点子。昨晚想通了。
    猜你喜欢
    • 1970-01-01
    • 2011-05-22
    • 1970-01-01
    • 1970-01-01
    • 2011-03-26
    • 2023-03-19
    • 2020-09-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多