/注册/没有这样的表的操作错误:CustomUser

问题描述 投票:0回答:0
models.py
from django.db import models

class CustomUser(models.Model):
    first_name = models.CharField(max_length=100)
    last_name = models.CharField(max_length=100)
    email = models.EmailField(unique=True)
    password = models.CharField(max_length=100)
    address = models.CharField(max_length=255)
    adharcard = models.CharField(max_length=12, unique=True)
    age = models.PositiveIntegerField()
    phone = models.CharField(max_length=15)

    class Meta:
        db_table = 'CustomUser'
    def __str__(self):
        return self.email
views.py
from django.contrib.auth import authenticate, login
from django.shortcuts import render, redirect
from django.http import HttpResponseRedirect
from django.contrib.auth.hashers import make_password
from django.views import View
from django.urls import reverse
from django.contrib.auth.decorators import login_required
from .models import CustomUser



def home(request):
    return render(request, 'app/login.html')


def register(request):
    if request.method == 'POST':
        first_name = request.POST.get('first_name')
        last_name = request.POST.get('last_name')
        email = request.POST.get('email')
        password = request.POST.get('password')
        address = request.POST.get('address')
        adharcard = request.POST.get('adharcard')
        phone = request.POST.get('phone')
        age = request.POST.get('age')
        hashed_password = make_password(password)

        user = CustomUser.objects.create(
            first_name=first_name,
            last_name=last_name,
            email=email,
            password=hashed_password,
            address=address,
            adharcard=adharcard,
            phone=phone,
            age=age
        )
        user.save()
        return redirect('app/login.html')
    return render(request, 'app/register.html')


class LoginView(View):
    """
    Login view for the user.

    Returns:
        Response: indicating the result of the login attempt.
            - If successful, redirects to the dashboard.
            - If the user is not found, renders the login page with an error message.
            - If the password is incorrect, renders the login page with an error message.
    """

    def get(self, request):
        return render(request, 'app/login.html')

    def post(self, request, *args, **kwargs):
        email = request.POST.get('email')
        password = request.POST.get('password')

        user = authenticate(request, email=email, password=password)

        if user is not None:
            login(request, user)
            return HttpResponseRedirect(reverse('app/dashboard.html'))
        else:
            return render(request, 'app/login.html', {'error_message': 'Invalid email or password'})

@login_required
def dashboard(request):
    user_first_name = request.user.first_name
    return render(request, 'app/dashboard.html', {'user_first_name': user_first_name})

错误:

OperationalError at /register/ no such table: CustomUser 上述异常(无此表:CustomUser)是导致以下异常的直接原因:D:\Platevisionx\myenv\Lib\site-packages\djang

django django-models django-views sqlite3-python
© www.soinside.com 2019 - 2024. All rights reserved.