2016-12-29 64 views
3

我在Django中创建了一个简单的表单,它将输入作为用户名,电子邮件,密码并将它们添加到数据库中。现在当我点击提交按钮时,URL调度器不会重定向以及更新数据库。这里是我的代码:URL调度器不工作

登录表单\ urls.py(登录表单作为项目):

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

app_name = 'authentication' 

urlpatterns = [ 
    url(r'^admin/', admin.site.urls), 
    url(r'^authentication/', include('authentication.urls',namespace="authentication")), 
    ] 

认证\ urls.py(身份验证的应用程序):

from django.conf.urls import url 
from .import views 

urlpatterns = [ 
    url(r'^$',views.SignIn, name="sign_in"), 
    url(r'^register/$',views.Register, name="register"), 
    ] 

sign_in.html

{% extends 'authentication/base.html' %} 

{% block body %} 

<div class="container"> 

    <div class="row"> 
     <div class="col-sm-offset-2 col-sm-8 col-md-offset-3 col-md-6 col-lg-offset-4 col-lg-4"> 
      <fieldset> 
       <legend> Register </legend> 
       <from method="post" action="{% url 'authentication:register' %}" > 
        {% csrf_token %} 
        <div > 
         <input name="username" type="username" placeholder="Username" class="form-control"> 
         <input name="email" type="email" placeholder="Email" class="form-control"> 
         <input name="password" type="password" placeholder="Password" class="form-control"> 
        </div> 
        <br> <button type="submit" class="btn btn-defaul"> Submit </button> 
       </from> 
      </fieldset> 
     </div> 
    </div> 

</div> 

{% endblock %} 

views.py

from django.shortcuts import render 
from .models import users 

def SignIn(request): 
    return render(request,'authentication/sign_in.html') 

def Register(request): 
    register = users() 
    register.username = request.POST['username'] 
    register.email = request.POST['email'] 
    register.password = request.POST['password'] 
    register.save() 
    return render(request,'authentication/profile.html',{'username': register.username }) 

感谢你的好意:)

+1

您是否曾尝试向“SignIn”和“Register”视图添加日志记录以确保它们被调用? – 2016-12-29 08:51:15

+1

在您的模板中,'

'标签被拼写为''。 –

+0

成功注册后,您尝试在哪个URL上重定向。 –

回答

1

Firestly更新您的视图:

from django.shortcuts import render,redirect 
from .models import users 

def SignIn(request): 
    return render(request,'authentication/sign_in.html') 

def Register(request): 
    register = users() 
    if request.method == 'POST': 
     register.username = request.POST['username'] 
     register.email = request.POST['email'] 
     register.password = request.POST['password'] 
     register.save() 
     return redirect('/your_new_url') 
    return render(request,'authentication/profile.html',{'username': register.username }) 

其次更新HTML模板代码到温控功能的形式as:

<form method="post" action="{% url 'authentication:register' %}" > 
    {% csrf_token %} 
    <div > 
     <input name="username" type="username" placeholder="Username" class="form-control"> 
     <input name="email" type="email" placeholder="Email" class="form-control"> 
     <input name="password" type="password" placeholder="Password" class="form-control"> 
    </div> 
    <br> 
    <button type="submit" class="btn btn-defaul"> Submit </button> 
</form> 
+0

它的工作,但我很困惑的意见 – Ahtisham

+0

它的工作,但我对你说的“返回重定向(...)”,然后再次调用返回呈现(...),但它是如何工作的意见迷惑?我的意思是你可以只从一个函数返回一次? – Ahtisham

+0

@Ahtisham是一次执行一次,但你需要明白,当request.method =='POST':是true时,只有返回重定向才会起作用,在其他情况下,返回呈现将起作用。 –