我正在尝试部署应用,但我的登录模板/视图出现Page not found(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。怎么解决这个问题?
答案 0 :(得分:1)
您正在重定向到相对网址:
return HttpResponseRedirect('profile.html')
这意味着,如果您目前位于/accounts/profile/
,系统会将您重定向到/accounts/profile/profile.html
。如果要重定向到名为 profile.html
的视图,您可以使用redirect
快捷方式:
from django.shortcuts import redirect
# ...
return redirect("profile.html")
大致相当于HttpResponseRedirect(reverse("profile.html"))
。
您的网址配置还有另一个问题:它包含一些这样的行:
url(r'^charts', 'survey.views.charts', name='charts'),
此行存在两个问题
网址末尾没有斜杠。最好是保持一致,并在所有URL的末尾加上斜杠
模式末尾没有行尾特殊字符$
。这意味着许多不同的网址将与模式匹配,例如/charts
,/charts/foo
,/chartsarecool
等等。
更好写
url(r'^charts/$', 'survey.views.charts', name='charts')
^^