gpt4 book ai didi

django - 未找到 'register' 的反向。 'register' 不是有效的 View 函数或模式名称

转载 作者:行者123 更新时间:2023-12-02 20:14:18 26 4
gpt4 key购买 nike

我正在尝试制作一个使用 django 内置用户模型的简单应用程序。我已经创建了一个注册页面,但是当我运行服务器时,我在索引页面上收到此错误。这是我正在使用的代码:

注册.html

<!DOCTYPE html>

{% extends "basic/base.html" %}

{% block title_block %}
<title>Registration</title>
{% endblock title_block %}

{% block body_block %}
<div class="jumbotron">
{% if registered %}
<h1>Thank you for registering</h1>
{% else %}
<h1>Register here!</h1>
<h3>Fill out the form: </h3>

<form enctype="multipart/form-data" method="post">
{% csrf_token %}
{{userForm.as_p}}
{{profileForm.as_p}}
<input type="submit" value="Register" name="">
</form>
{% endif %}
</div>
{% endblock body_block %}

“注册”方法的 Views.py

def register(request):

registered = False

if(request.method == 'POST'):
userForm = forms.UserForm(data=request.POST)
profileForm = forms.UserProfileInfoForm(data=request.POST)

if((userForm.is_valid()) and (profileForm.id_valid())):
user = userForm.save()
user.set_password(user.password)
user.save()

profile = profileForm.save(commit=False)
profile.user = user

if('profileImage' in request.FILES):
profile.profileImage = request.FILES['profileImage']

profile.save()

registered = True

else:
print(userForm.errors, profileForm.errors)

else:
userForm = forms.UserForm()
profileForm = forms.UserProfileInfoForm()

return render(request, 'basic/registration.html', {'userForm':userForm, 'profileForm':profileForm, 'registered':registered})

这是该项目的 urls.py

from django.contrib import admin
from django.urls import path, include
from basic import views

urlpatterns = [
path('', views.index, name='index'),
path('admin/', admin.site.urls),
path('basic/', include('basic.urls', namespace='basic'))
]

这是基本应用程序的 urls.py

from django.urls import path
from . import views

app_name = 'basic'

urlpatterns = [
path('register/', views.register)
]

以及base.html中页面的链接

<a class="nav-link" href="{% url 'basic:register' %}">Register</a>

什么可能导致这里出现错误?

最佳答案

您必须在注册路由中包含一个 name 参数。

path('register/', views.register, name='register')

https://docs.djangoproject.com/en/2.1/topics/http/urls/#reverse-resolution-of-urls

关于django - 未找到 'register' 的反向。 'register' 不是有效的 View 函数或模式名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52747072/

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