English 中文(简体)
/ 账户/ 登记/ 挂载/ 挂载/ 挂载/ 挂载/ 挂载/ 挂载/ 挂载/ 挂载/ 挂载/ 挂载/ 挂载/ 挂载/ 挂载/ 挂载/ 挂载/ 挂载/ 挂载/ 挂载/ 挂载/ 挂载/ 挂载/ 挂载/ 挂载/ 挂载/ 挂载/ 挂载/ 挂载/ 挂载/ 挂载/ 挂载/ 挂载/ 挂载/ 挂载/ 挂载/ 挂载/ 挂载/ 挂载/
原标题:error in django KeyError at /accounts/register/

I have a problem. When clicking on the link http://127.0.0.1:8000/accounts/register / returns the KeyError error at /accounts/register/ email . I program in django in pycharm

这是代码 :

登记/转录:

from django.contrib.auth import views as auth_views
from django.urls import path
from . import views

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

登记/查看.py:

from django.shortcuts import render
from django.contrib.auth import login, authenticate
from django.shortcuts import render, redirect
from .forms import RegistrationForm
from django.shortcuts import render, redirect


def sign_up(request):
    if request.method ==  POST :
        form = RegistrationForm(request.POST)
        if form.is_valid():
            user = form.save()
            login(request, user)
            return redirect( home )
        else:
            form = RegistrationForm()
            return render(request,  registration/register.html , { form : form})


def register():
    print("hello!")

urls.py(项目本身):

from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
from django.contrib.auth import views as auth_views
from django.urls import path

urlpatterns = [
    path( admin/ , admin.site.urls),
    path(  , include( main.urls )),
    path( news/ , include( news.urls )),
    path( accounts/ , include( django.contrib.auth.urls )),
]

urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

登记册. html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Регистрация</title>
</head>
<body>
    <h1>Регистрация</h1>
    <form method="post" action="{% url  register  %}">
        {% csrf_token %}
        {{ form.email }}
        {{ form.password1 }}
        {{ form.password2 }}
        <button type="submit">Зарегистрироваться</button>
    </form>
</body>
</html>

窗体.py:

from django import forms
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm


class RegistrationForm(UserCreationForm):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields[ email ].label =  Email address 
        self.fields[ password1 ].label =  Password 
        self.fields[ password2 ].label =  Confirm Password 


class Meta:
    model = User
    fields = ( email ,  password1 ,  password2 )

我试过很多YouTube视频和文件

问题回答

您有一个名为注册的应用程序 。

您的工程( 顶层) urls. py 不包括注册/ urpy 的引用, 所以注册 url 未知

您需要包含以下内容:

 path("registration/", include("registration.urls")),

您可使用





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...