Django一直在渲染同一页面

时间:2017-11-12 04:38:11

标签: python html django

我正在尝试使用Django设置导航,但是,每次我尝试导航它都会再次返回到同一页面。 请帮助,任何建议将不胜感激。谢谢!

views.py

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.shortcuts import render

from django.contrib.auth import login, authenticate
from django.contrib.auth.forms import UserCreationForm
from django.shortcuts import render, redirect
#from django.contrib.auth import views as auth_views

#from . import views
# Create your views here.

def login(request):
    return render(request, 'login.html')

def signup(request):
    if request.method == 'POST':
        form = UserCreationForm(request.POST)
        if form.is_valid():
            form.save()
            #username = form.cleaned_data.get('username')
            #raw_password = form.cleaned_data.get('password1')
            #user = authenticate(username=username, password=raw_password)
            #login(request, user)
            return redirect('/templates')
        else:
            form = UserCreationForm()
        return render(request, 'templates/signup_form.html', {'form': form})

在Project中调用该文件 urls.py

from django.conf.urls import url, include
from django.contrib import admin

urlpatterns = [
    url(r'^login/', include('website.urls')),
    url(r'^signup/', include('website.urls')),
    url(r'^admin/', admin.site.urls),

]

在应用网站urls.py

from django.conf.urls import url
from django.contrib.auth.views import login
from . import views
#
urlpatterns = [
    url(r'^', views.login, name=''),
    url(r'^login/', login, {'template_name': 'templates/login.html'}),
    url(r'^signup/', views.signup, name = 'signup')




    #url(r'^login/$', index,{{'template_name': 'templates/index.html' }})
]

1 个答案:

答案 0 :(得分:1)

网址格式从上到下匹配。您的第一个urlpattern匹配它看到的任何内容,因此django会将所有请求发送到views.login

尝试将该行放在列表的底部

urlpatterns = [

    url(r'^login/', login, {'template_name': 'templates/login.html'}),
    url(r'^signup/', views.signup, name = 'signup')

    url(r'^', views.login, name=''),}})