ImportError: attempted relative import beyond top-level package in Django while importing from different django apps

44 Views Asked by At

I am trying to import SignUp model from authentication app into views file of another app named core.

Why i am getting this error and how can i solve this?

core/views.py

from django.http import HttpResponse
from django.shortcuts import render
from mongoengine import DoesNotExist

from ..authentication.models import SignUp


def home(request):
    default_content = {
        'title': 'Welcome to College Management System',
    }
    # Retrieve the user_id and username from cookies
    user_id = request.COOKIES.get('user_id')
    username = request.COOKIES.get('username')

    if user_id:
        try:
            user = SignUp.objects.get(username=username)
            # Your existing code that uses user_id goes here
            # For example, you can fetch additional user data using user_id
            # Update default_content or perform other actions based on user information
        except DoesNotExist:
            # Handle the case where the user does not exist
            return HttpResponse("User does not exist")

    return render(request, 'core/base.html', {'content': default_content, 'username': username})

authentication/models.py

import mongoengine
from django.contrib.auth.hashers import check_password


class SignUp(mongoengine.Document):

    email = mongoengine.EmailField()
    username = mongoengine.StringField(max_length=15)
    password = mongoengine.StringField()
    first_name = mongoengine.StringField(max_length=20)
    last_name = mongoengine.StringField(max_length=20)
    role = mongoengine.StringField(choices=('admin', 'faculty', 'student'))

    meta = {
        'collection': 'Signup',
    }

    def check_password(self, raw_password):
        return check_password(raw_password, self.password)

enter image description here

0

There are 0 best solutions below