Path Parakh
Path Parakh

Reputation: 39

No module named 'ecommerce.store' in django

I run command python3 manage.py runserver It throws an error showing that no module named ecommerce.store but it is.

This is my ecommerce.store.views file

    from ecommerce.store.models import Product
from django.shortcuts import render
from .models import *  

def store(request):
    products = Product.objects.all()
    context = {'products':products}
    return render(request, 'store/store.html', context)

def cart(request):
    context = {}
    return render(request, 'store/cart.html', context)

def checkout(request):
    context = {}
    return render(request, 'store/checkout.html', context)

This is my ecommerce.store.admin file

    from django.contrib import admin
from .models import *

admin.site.register(Customer)
admin.site.register(Product)
admin.site.register(Order)
admin.site.register(OrderItem)
admin.site.register(ShippingAddress)

This is my ecommerce.store.models file

from django.db import models
from django.contrib.auth.models import User
from django.db.models.fields.related import ForeignKey

class Customer(models.Model) :
    user = models.OneToOneField(User, on_delete=models.CASCADE, null=True, blank=True)
    name = models.CharField(max_length=200, null=True)
    email = models.CharField(max_length=200, null=True)

    def __str__(self) :
        return self.name

class Product(models.Model) :
    name = models.CharField(max_length=200, null=True)
    price = models.FloatField()
    digital = models.BooleanField(default=False, null=True, blank=True)

    def __str__(self) :
        return self.name

class Order(models.Model) :
    customer = models.ForeignKey(Customer, on_delete=models.SET_NULL, blank=True, null=True)
    date_ordered = models.DateTimeField(auto_now_add=True)
    complete = models.BooleanField(default=False, null=True, blank=True)
    transaction_id = models.CharField(max_length=200, null=True)

    def __str__(self) :
        return str(self.id)

class OrderItem(models.Model) :
    product = models.ForeignKey(Product, on_delete=models.SET_NULL, blank=True, null=True)
    order = models.ForeignKey(Order, on_delete=models.SET_NULL, blank=True, null=True)
    quantity = models.IntegerField(default=0, blank=True, null=True)
    date_added = models.DateTimeField(auto_now_add=True)

class ShippingAddress(models.Model) :
    customer = models.ForeignKey(Customer, on_delete=models.SET_NULL, blank=True, null=True)
    order = models.ForeignKey(Order, on_delete=models.SET_NULL, blank=True, null=True)
    address = models.CharField(max_length=200, null=False)
    city = models.CharField(max_length=200, null=False)
    state = models.CharField(max_length=200, null=False)
    zipcode = models.CharField(max_length=200, null=False)
    date_added = models.DateTimeField(auto_now_add=True)

    def __str_(self):
        return self.address

This is my ecommerce.store.urls file

from django.urls import path
from . import views

urlpatterns = [
    path('', views.store, name="store"),
    path('cart/', views.cart, name="cart"),
    path('checkout/', views.checkout, name="checkout"),
]

This is the image of the actual error showing in terminal please help me enter image description here

Thanks for helping me

Upvotes: 2

Views: 689

Answers (3)

Chuks
Chuks

Reputation: 59

I had a similar issue, found out that I did not import the forms into views.py header file.

Check to make sure that this has been imported

Upvotes: 0

fitz
fitz

Reputation: 580

from ecommerce.store.models import Product will find package ecommerce from project root dir, this will find the subdirectory ecommerce not root directory ecommerce, you can use absolute import:

from store.models import Product

or just use relative import(recommended way):

from .models import Product

Upvotes: 1

LucasBorges-Santos
LucasBorges-Santos

Reputation: 392

try change your wrong import:

from ecommerce.models import Product

Upvotes: 0

Related Questions