我正在尝试为我的网站制作结帐表格。 我收到一条错误消息,提示没有名为“ core.forms”的模块。
这是我的文件:
View.py
from django.contrib import messages
from django.core.exceptions import ObjectDoesNotExist
from django.contrib.auth.decorators import login_required
from django.contrib.auth.mixins import LoginRequiredMixin
from django.shortcuts import render, get_object_or_404
from django.views.generic import ListView, DetailView, View
from django.shortcuts import redirect
from django.utils import timezone
from .forms import CheckoutForm
from .models import Item, OrderItem, Order
class CheckoutView(View):
def get(self, *args, **kwargs):
form = CheckoutForm()
context = {
'form': form
}
return render(self.request, "checkout.html", context)
def post(self, *args, **kwargs):
form = CheckoutForm(self.request.POST or None)
if form.is_valid():
print("The form is valid")
return redirect('core:checkout')
Form.py
from django import forms
from django_countries.fields import CountryField
class CheckoutForm(forms.Form):
street_address = forms.CharField()
apartment_address = forms.CharField(required=False)
country = CountryField(blank_label='(select country)')
zip = forms.CharField()
same_billing_address = forms.BooleanField(widget=forms.CheckboxInput())
save_info = forms.BooleanField(widget=forms.CheckboxInput())
payment_option = forms.BooleanField(widget=forms.RadioSelect())
运行python manage.py runserver
时出错msg
File "C:\Users\Dell\project5\core\urls.py", line 2, in <module>
from .views import (
File "C:\Users\Dell\project5\core\views.py", line 9, in <module>
from .forms import CheckoutForm
ModuleNotFoundError: No module named 'core.forms'
我是Django的新手
Plz,帮助
答案 0 :(得分:1)
正如我在评论中所说,问题在于您正在导入from .forms
,但是您的文件名为Form.py
。
因为文件以这种方式命名,所以无法从那里导入。
如果将文件名从Form.py
更改为forms.py
,则应该可以。