找不到“注册”的反面。 'register'不是有效的视图函数或模式名称

时间:2018-10-10 18:59:28

标签: django django-templates django-views

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

Registration.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>

什么在这里会导致错误?

2 个答案:

答案 0 :(得分:2)

尝试一下(在您的html文件中)

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

和您的urls.py(在您的应用程序内部)如下:

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

这种方法对我有用,我遇到了同样的问题。

答案 1 :(得分:0)

您必须在寄存器路由中包含一个ExcelReaders参数。

xxxx

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