Django:无法导入名称 'model'
Django: Cannot import name 'model'
所以我是一名学生,我正在尝试使用 Django 建立一个小商店,所以当我 运行 我的笔记本电脑上的项目按预期工作但是当我将它上传到服务器时我得到了这个错误
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
这是我的文件结构图
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'
从模型导入 CustomerOrderModel
试试这个:
from HomePage.models import CustomerOrderModel
我明白了,这是由于您 urls.py 中的循环导入所致
你在做
from . import views
你应该这样做
from .views import XyzView, AbcView
这是因为您还导入了在 views.py 中完成的所有导入。
希望你明白了。
所以我是一名学生,我正在尝试使用 Django 建立一个小商店,所以当我 运行 我的笔记本电脑上的项目按预期工作但是当我将它上传到服务器时我得到了这个错误
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
这是我的文件结构图
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'
从模型导入 CustomerOrderModel
试试这个:
from HomePage.models import CustomerOrderModel
我明白了,这是由于您 urls.py 中的循环导入所致
你在做
from . import views
你应该这样做
from .views import XyzView, AbcView
这是因为您还导入了在 views.py 中完成的所有导入。 希望你明白了。