djangodjango-modelsdjango-viewssqlite3-python

OperationalError at /register/ no such table: CustomUser


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})

Error:

OperationalError at /register/ no such table: CustomUser The above exception (no such table: CustomUser) was the direct cause of the following exception: D:\Platevisionx\myenv\Lib\site-packages\django\core\handlers\exception.py, line 55, in inner response = get_response(request) ^^^^^^^^^^^^^^^^^^^^^ … Local vars D:\Platevisionx\myenv\Lib\site-packages\django\core\handlers\base.py, line 197, in _get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ … Local vars D:\Platevisionx\platevision\app\views.py, line 28, in register user = CustomUser.objects.create( … Local vars

I cant Solve the error I have ran python manage.py makemigrations and python manage.py migrate but it is not working for me.
Please anyone tell me the problem I an using sqlite3 in Django


Solution

  • set db_table = 'custom_user' and again run migration. Try this method.