2

So I am a student and I am trying to build a small shop using Django, so when I run the project on my laptop it works as expected but when I upload it to the server I got this error

cannot import name 'CustomerOrderModel'

views.py

from django.shortcuts import render, get_object_or_404
from django.views import generic
from HomePage.models import Product
from HomePage.forms import Customer
from django.contrib import messages
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger

#other functions

def customer_order_view(request, pk):
   product = get_object_or_404(Product, id=pk)
   form = Customer(request.POST or None)
   if form.is_valid():
      instance = form.save(commit=False)
      instance.customer_product_name = product
      instance.save()
      messages.success(request, "Success \n we will contact you soon")
      context = {'form': form, 'product': product}
   return render(request, 'HomePage/customerDetail.html', context)

forms.py

from django import forms
from HomePage.models import CustomerOrderModel  #the error in this line 

class Customer(forms.ModelForm):
  class Meta:   
    model = CustomerOrderModel   
    fields = [# some fields]

url.py

from django.conf.urls import url
from django.conf import settings
from django.conf.urls.static import static
from HomePage.views import index_view, GiftView, FlowerView, BalloonView, 
detail_view, customer_order_view

app__name = 'HomePage'

urlpatterns = [
              url(r'^$', index_view, name='index'),
              url(r'^gift/$', GiftView.as_view(), name='gift'),
              url(r'^flower/$', FlowerView.as_view(), name='flower'),
              url(r'^balloon/$', BalloonView.as_view(), name='balloon'),
              url(r'^(?P<pk>[0-9]+)/$', detail_view, name='detail'),
              url(r'^(?P<pk>[0-9]+)/order/$', customer_order_view, 
                  name='product_form'),
                  ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

admin.py

from django.contrib import admin
from HomePage.models import Product, CustomerOrderModel

#some code

models.py

from django.db import models
from django.core.validators import RegexValidator

#some code

this is picture of my file structure

image of the file structure

The Traceback

  File "/home/django/venv/lib/python3.5/site-packages/django/core/handlers/exception.py" in inner
  35.             response = get_response(request)

File "/home/django/venv/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
 128.                 response = self.process_exception_by_middleware(e, request)

File "/home/django/venv/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
  126.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/home/django/LovelyMemory/HomePage/views.py" in customer_order_view
49. def customer_order_view(request, pk):

File "/home/django/LovelyMemory/HomePage/forms.py" in <module>
4. class Customer(forms.ModelForm):

File "/home/django/LovelyMemory/HomePage/forms.py" in Customer
5.     class Meta:

File "/home/django/LovelyMemory/HomePage/forms.py" in Meta
6.         from HomePage.models import CustomerOrderModel

Exception Type: ImportError at /8/order/
Exception Value: cannot import name 'CustomerOrderModel'
13
  • Possible duplicate of ImportError: cannot import name <model_class> Commented May 19, 2018 at 10:58
  • I looked at this question but I don't think it is the same because my project run on my laptop. @NinjaWarrior11 Commented May 19, 2018 at 11:08
  • 1
    if it is model.py then use from .model import CustomerOrderModel Commented May 19, 2018 at 11:19
  • 1
    You somehow did not name your file models.py but model.py? Commented May 19, 2018 at 11:27
  • 1
    Error might not from this, so can you please post complete traceback ? That will be helpful Commented May 19, 2018 at 11:50

3 Answers 3

2

I can see, this is due to circular import in your urls.py You are doing from . import views You should do like from .views import XyzView, AbcView

This is because you are importing all imports done in views.py as well. Hope you got the idea.

Sign up to request clarification or add additional context in comments.

17 Comments

ok, I fixed every import that I have the way you said it. but I still can't import the model. the question has been updated it. I am really stuck in this problem for a long time. -_- @SumeetKumar
on what server you are hosting your website ?
I used this turorial to build the server, by the way, the code runs on my laptop with no problem @SumeetKumar
@waleed can you please share your urls.py, forms.py and views.py along with all the imports you are doing, let me give you a hint, python is interpreted language, sequence of your import may also affect the output depending on python version.
if you are also using same form in your admin.py ? the you have to organize imorts in admin.py too
|
2

from model import CustomerOrderModel

3 Comments

I tried "from myproject.model import CustomerOrderModel" but it didn't work also
@waleed This answer should work. Can you please add your directory structure to your post.
I added a picture of the app structure @NinjaWarrior11
0

Try this:

from HomePage.models import CustomerOrderModel

1 Comment

I tried it and got the same result, bear in mind that it doesn't work on the server only when I run it on a local server on my laptop it works fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.