2016-12-29 50 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時,只有返回重定向纔會起作用,在其他情況下,返回呈現將起作用。 –