Django:无法导入名称' model'

时间:2018-05-19 10:56:58

标签: python django

所以我是一名学生,我正在尝试使用Django建立一个小商店,所以当我在我的笔记本电脑上运行该项目时,它按预期工作,但当我将其上传到服务器时,我收到此错误

  

无法导入名称' 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

这是我的文件结构图片

image of the file structure

追溯

  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'

3 个答案:

答案 0 :(得分:2)

来自模型导入CustomerOrderModel

答案 1 :(得分:0)

试试这个:

from HomePage.models import CustomerOrderModel

答案 2 :(得分:0)

我可以看到,这是由于你的urls.py中的循环导入 你在做 from . import views 你应该这样做 from .vies import XyzView, AbcView

这是因为您要导入在views.py中完成的所有导入。 希望你明白了。