我遇到了不存在的網址能夠顯示的問題。例如,我去/ myapp /登錄一個網址,它的工作原理。然後我去/ myapp/login /登錄,它仍然有效。然後我去/ myapp/insertrandomcharacters這裏/登錄,它仍然工作。這將適用於我的子路徑/ myapp/so/myapp/register,/ myapp/login和/ myapp/myaccount中的每個url。看來只要它以我的urlconf中的名字結尾,它就可以工作。我一直無法解決這個問題,所以我從我的登錄頁面和我的urlconf中添加了一些代碼,希望能夠解決這個問題。沒有定義的django網址
我應該404任何不是/ MyApp的/,/ MYAPP /登錄/,/ MYAPP /註冊/,/ MyApp的/我的帳戶/
http://paste.pound-python.org/show/31613/
#django/urls.py
urlpatterns = patterns('',
url(r'myapp/', include('myapp.urls')),
url(r'^admin/', include(admin.site.urls)),
)
#myapp/urls.py
urlpatterns = patterns('myapp.views',
url(r'^$', 'index', name='index'),
url(r'register/$', 'register', name='register'),
url(r'myaccount/$', 'myaccount', name='myaccount'),
url(r'login/$', 'login_page', name='login'),
url(r'logout/$', 'logout_user', name='logout')
)
#myapp/login.py
def login_page(request):
if request.user.is_authenticated():
return HttpResponseRedirect(reverse('index'))
form = LoginForm(request.POST or None)
if form.is_valid():
username = form.cleaned_data['username']
username = username.capitalize()
password = form.cleaned_data['password']
try:
User.objects.get(username=username)
except:
return render(request, 'myapp/login.html', {
'form': form,
'error': "Account doesn't exist"
})
user = authenticate(username=username, password=password)
if user is not None:
if user.is_active:
login(request, user)
return HttpResponseRedirect(reverse('index'))
else:
return render(request, 'myapp/login.html', {
'form': form,
'error': 'Account is disabled or banned',
})
else:
return render(request, 'myapp/login.html', {
'form': form,
'form': 'Authentication Error!'
})
return render(request, 'myapp/login.html', {
'form': form,
})
{% if not user.is_authenticated %}
#myapp/templates/myapp/login.html
<strong>Login</strong>
<form action='{% url 'login' %}' method="post">{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Login">
</form>
{{ error }}
{% endif %}
您定義'login_page'在'MYAPP/login.py'然後你用'myapp.views'前綴是指它'urls.py',怎麼辦? – 2013-03-25 13:12:16