【问题标题】:url doesn't match with url fileurl 与 url 文件不匹配
【发布时间】:2014-01-06 02:43:12
【问题描述】:

我正在尝试部署应用程序,但我的登录模板/视图出现页面未找到 (404) 错误。但同样的代码在 localhost 上也能工作。

这是错误信息:

The current URL, accounts/profile/profile.html, didn't match any of these.

网址文件:

urlpatterns = patterns('',
    # Examples:
    url(r'^$', 'survey.views.home', name='home'),
    url(r'^survey/(?P<id>\d+)/$', 'survey.views.SurveyDetail', name='survey_detail'),
    url(r'^confirm/(?P<uuid>\w+)/$', 'survey.views.Confirm', name='confirmation'),
    url(r'^privacy/$', 'survey.views.privacy', name='privacy_statement'),
    url(r'^admin/', include(admin.site.urls)),
    url(r'^piechart/$', 'survey.views.piechart', name = 'chart_code.html'),
    url('^accounts/', include('registration.urls')),
    url('^accounts/login/$', 'survey.views.login'),
    url('^accounts/auth/$', 'survey.views.auth_view'),
    **url('^accounts/profile/$', 'survey.views.profile'),**
    url('^accounts/logout/$', 'django.contrib.auth.views.logout'),
    url(r'^map/$','survey.views.javamap', name = 'chart_code.html'),
    url(r'^charts', 'survey.views.charts', name='charts'),
    url(r'^login/$', 'django.contrib.auth.views.login'),
    url(r'^accounts/auth/profile', 'survey.views.profile', name = 'profile.html'),
    url(r'^profile', 'survey.views.profile', name = 'profile.html'),
    url(r'^accounts/auth/results', 'survey.views.survey_name', name = 'results.html'),
    url(r'^answer_survey', 'survey.views.answer_survey'),
    url(r'^results/(?P<id>\d+)/$', 'survey.views.SurveyResults', name='results'),


)

个人资料视图:

@login_required
def profile(request):
    user = request.user
    if user.is_authenticated():
        n_survey = Survey.objects.filter(user = user)
        if n_survey:
            print "*---------- str " + str(n_survey)
            for survey in n_survey:
                print "survey id " + str(survey.id)
            n = len(n_survey)
            print "n " + str(n)
            return render(request, 'profile.html')
        else:
            print("*---------- sem surveys para print")
            return HttpResponseRedirect('profile.html')
    else:
        msg = "Usuario ou senha digitados incorretamente"
        return HttpResponseRedirect('home.html', {'msg': msg})

在 localhost 中,URL 帐户/配置文件匹配,因为 django 最后不包含 profile.html。如何解决?

【问题讨论】:

  • 我建议你不要把“.html”放在你的 url 的 name 参数中,因为它使代码看起来像一个相对 url 而不是一个名字.

标签: django django-templates django-views


【解决方案1】:

您正在重定向到相对 URL:

return HttpResponseRedirect('profile.html')

这意味着,如果您当前位于 /accounts/profile/,您将被重定向到 /accounts/profile/profile.html。如果您想重定向到 named profile.html 的视图,例如,您可以使用 redirect 快捷方式:

from django.shortcuts import redirect
# ...
return redirect("profile.html")

大致相当于HttpResponseRedirect(reverse("profile.html"))

您的 url 配置中还有另一个问题:它包含如下几行:

url(r'^charts', 'survey.views.charts', name='charts'),

这条线有两个问题

  1. URL 末尾没有斜杠。最好是一致的,并且在所有 URL 的末尾都有一个斜杠

  2. 模式末尾没有行尾特殊字符$。这意味着许多不同的 URL 都会匹配该模式,例如 /charts/charts/foo/chartsarecool 等等。

写得更好

url(r'^charts/$', 'survey.views.charts', name='charts')
             ^^

【讨论】:

  • profile.html 是一个模板。为什么django在url中添加字符串profile.html?
  • 因为你告诉它:HttpResponseRedirect('profile.html')HttpResponseRedirect 总是 takes a URL or path as its first argument
  • profile.html 不仅是一个模板,您还可以将其用作您的 url 模式的名称:url(r'^accounts/auth/profile', 'survey.views.profile', name = 'profile.html'),
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-08-24
  • 2021-06-21
  • 1970-01-01
  • 1970-01-01
  • 2013-05-05
  • 2012-08-03
  • 2021-01-21
相关资源
最近更新 更多