我正在尝试使客户和员工登录并注册页面...但是,每当我尝试转到localhost:8000 / customer register时,我都会得到一个页面未找到错误,未找到错误。这是我的
urls.py文件:
from django.contrib import admin
from django.urls import path
from accounts import views
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.register, name = 'register'),
path('customer_resgister', views.customer_register.as_view(), name = 'customer_register'),
]
views.py:
from django.shortcuts import render
from django.views.generic import CreateView
from .models import User, Customer, Employee
from .forms import CustomerSignupForm, EmployeeSignupForm
# Create your views here.
def register(request):
return render(request, 'accounts/register.html')
class customer_register(CreateView):
model = User
form_class = CustomerSignupForm
template_name = 'accounts/customer_register.html'
#def customer_register(request):
models.py:
from django.db import models
from django.contrib.auth.models import AbstractUser
# Create your models here.
class User(AbstractUser):
is_customer = models.BooleanField(default=False)
is_employee = models.BooleanField(default=False)
first_name = models.CharField(max_length = 150)
last_name = models.CharField(max_length = 150)
class Customer(models.Model):
user = models.OneToOneField(User, on_delete = models.CASCADE, primary_key = True)
Phone_no = models.CharField(max_length = 10)
location = models.CharField(max_length = 150)
class Employee(models.Model):
user = models.OneToOneField(User, on_delete = models.CASCADE, primary_key = True)
Phone_no = models.CharField(max_length = 10)
designation = models.CharField(max_length = 150)
forms.py:
from django.contrib.auth.forms import UserCreationForm
from django.db import transaction
from .models import Customer, Employee, User
from django import forms
class CustomerSignupForm(UserCreationForm):
first_name = forms.CharField(required = True)
last_name = forms.CharField(required = True)
phone_no = forms.CharField(required = True)
class Meta(UserCreationForm.Meta):
model = User
@transaction.atomic
def data_save(self):
user = super().save(commit = False)
user.first_name = self.cleaned_data.get('first_name')
user.last_name = self.cleaned_data.get('last_name')
user.save()
customer = Customer.objects.create(user = user)
customer.phone_no = self.cleaned_data.get('phone_no')
customer.save()
return user
class EmployeeSignupForm(UserCreationForm):
first_name = forms.CharField(required = True)
last_name = forms.CharField(required = True)
designation = forms.CharField(required = True)
class Meta(UserCreationForm.Meta):
model = User
@transaction.atomic
def data_save(self):
user = super().save(commit = False)
user.first_name = self.cleaned_data.get('first_name')
user.last_name = self.cleaned_data.get('last_name')
user.save()
employee = Employee.objects.create(user = user)
employee.phone_no = self.cleaned_data.get('phone_no')
employee.designation = self.cleaned_data.get('designation')
customer.save()
return user
settings.py:
from pathlib import Path
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.1/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'apk-h*1!*f-=^6zw^_q0q!z6att9f+exfr+k(!awfvybu^x(l%'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []
AUTH_USER_MODEL = 'accounts.User'
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'accounts'
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'demo_register.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'demo_register.wsgi.application'
# Database
# https://docs.djangoproject.com/en/3.1/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}
# Password validation
# https://docs.djangoproject.com/en/3.1/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# Internationalization
# https://docs.djangoproject.com/en/3.1/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.1/howto/static-files/
STATIC_URL = '/static/'
我所有的模板都存储在帐户/模板/帐户中
我不知道我在做什么错...请帮助。
答案 0 :(得分:0)
我认为问题不在于您的模板。网址无法映射到正确的视图时,会显示404错误。 您可以共享错误页面摘要,也可以尝试为客户注册的网址路径添加斜杠
答案 1 :(得分:0)
您在url_patterns的第三个路径中有一个错字,应该注册而不是'resgister'。
使用空格代替下划线
url_patterns = [ path('客户注册',views.customer_register.as_view(),name ='customer_register') ]