gpt4 book ai didi

python - 为什么 URL 调度程序既不重定向也不更新数据库?

转载 作者:行者123 更新时间:2023-11-28 18:26:08 26 4
gpt4 key购买 nike

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

loginForm\urls.py(作为项目的 loginForm):

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(应用身份验证):

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 })

最佳答案

FireSTLy 将您的 View 更新为:

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>

关于python - 为什么 URL 调度程序既不重定向也不更新数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41376072/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com