我在Django中创建一个简单的表单,它将输入作为用户名,电子邮件,密码并将它们添加到数据库中。现在,当我单击“提交”按钮时,URL调度程序不会重定向以及更新数据库。这是我的代码:
loginForm \ urls.py(loginForm as project):
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")),
]
authentication \ urls.py(身份验证为app):
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 })
谢谢你的善意:)
答案 0 :(得分:1)
将您的观点更新为:
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模板代码,以便将表单更改为:
<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>