提交表单时Django错误

loading...


-1

执行系统检查......

线程中未处理的异常由.wrapper在0x75abfcd8> Traceback(最近一次调用最后一次)启动:文件“/home/pi/.venv/lib/python3.5/site-packages/django/utils/autoreload.py”,第225行,在包装器fn(* args,** kwargs)文件“/home/pi/.venv/lib/python3.5/site-packages/django/core/management/commands/runserver.py”,第121行,在inner_run中self.check(display_num_errors = True)文件“/home/pi/.venv/lib/python3.5/site-packages/django/core/management/base.py”,第364行,检查include_deployment_checks = include_deployment_checks,文件“ /home/pi/.venv/lib/python3.5/site-packages/django/core/management/base.py“,第351行,在_run_checks中返回checks.run_checks(** kwargs)文件”/ home / pi / .venv / lib / python3.5 / site-packages / django / core / checks / registry.py“,第73行,在run_checks中new_errors = check(app_configs = app_configs)文件”/home/pi/.venv/lib/python3 .5 / site-packages / django / core / checks / urls.py“,第40行,在check_url_namespaces_unique中all_namespaces = _load_all_namespaces(解析程序)文件”/ home / pi / .venv / lib / python3.5 / site-packages / django / core / checks / urls.py“,第57行,在_load_all_namespaces中url_patterns = getattr(resolver,'url_patterns',[])文件”/ home / pi / . venv / lib / python3.5 / site-packages / django / utils / functional.py“,第36行,在get res = instance.dict [self.name] = self.func(instance)File”/ home / pi / .venv / lib / python3.5 / site-packages / django / urls / resolvers.py“,第536行,在url_patterns patterns = getattr(self.urlconf_module,”urlpatterns“,self.urlconf_module)文件”/ home / pi / .venv / lib / python3.5 / site-packages / django / utils / functional.py“,第36行,在get res = instance.dict [self.name] = self.func(instance)File”/ home / pi /.venv/lib/python3.5/site-packages/django/urls/resolvers.py“,第529行,在urlconf_module中返回import_module(self.urlconf_name)文件”/home/pi/.venv/lib/python3.5 /importlib/init.py“,第126行,在import_module中返回_bootstrap._gcd_import(name [level:],package,level)文件”“,第986行,在_gcd_import文件”“,第969行,在_find_and_load文件”“中, 958行_find_and_load_unlocked文件“”,第673行,在_load_unlocked文件“”,第673行,在exec_module文件“”,第222行,在_call_with_frames_removed文件“/home/pi/tango_with_django_project/tango_with_django_project/urls.py”,第25行,在url中( r'^ rango /',include('rango.urls')),文件“/home/pi/.venv/lib/python3.5/site-packages/django/urls/conf.py”,第34行,in include urlconf_module = import_module(urlconf_module)文件“/home/pi/.venv/lib/python3.5/importlib/init.py”,第126行,在import_module中返回_bootstrap._gcd_import(name [level:],package,level)文件“”,行986,在_gcd_import文件“”,第969行,在_find_and_load文件“”,第958行,在_find_and_load_unlocked文件“”,第673行,在_load_unlocked文件“”,第673行,在exec_module文件“”,行222,在_call_with_frames_removed文件“/home/pi/tango_with_django_project/rango/urls.py”,第2行,来自rango导入视图文件“/home/pi/tango_with_django_project/rango/views.py”,第5行,来自rango .forms import CategoryForm File“/ home / pi /tango_with_django_project/rango/forms.py“,第4行,在类CategoryForm(forms.ModelForm)中:文件”/home/pi/.venv/lib/python3.5/site-packages/django/forms/models.py“ ,第243行,新的“需要更新” . %name django.core.exceptions.ImproperlyConfigured:禁止创建没有'fields'属性或'exclude'属性的ModelForm;表单CategoryForm需要更新 .

这是我的forms.py

来自rango.models导入页面,类别的django导入表单

class CategoryForm(forms.ModelForm):name = forms.CharField(max_length = 128,help_text =“请输入类别名称 . ”)views = forms.IntegerField(widget = forms.HiddenInput(),initial = 0)likes = forms .IntegerField(widget = forms.HiddenInput(),initial = 0)

class Meta:model = Category class PageForm(forms.ModelForm):title = forms.CharField(max_length = 128,help_text =“请输入页面 Headers . ”)url = forms.URLField(max_length = 200,help_text =“请输入页面的URL . “)views = forms.IntegerField(widget = forms.HiddenInput(),initial = 0)

class Meta:model = Page fields =('title','url','views')这里是我的views.py

来自django.template从django.shortcuts导入RequestContext从rango.models导入来自rango.models的render_to_response从rango.models导入页面导入来自rango.forms的页面导入CategoryForm

def index(request):context = RequestContext(request)category_list = Category.objects.order_by(' - likes')[:5] context_dict = {'categories':category_list} category_list中的类别:category.url = category.name .replace('','_')返回render_to_response('rango / index.html',context_dict,context)

def about(request):context = RequestContext(request)context_dict = {'boldmessage':“我是上下文中的粗体字体”} return render_to_response('rango / about.html',context_dict,context)

def category(request,category_name_url):context = RequestContext(request)category_name = category_name_url.replace('_','')context_dict = {'category_name':category_name} try:category = Category.objects.get(name = category_name)pages = Page.objects.filter(category = category)context_dict ['pages'] = pages context_dict ['category' ] = = Category.DoesNotExist除外:传递返回render_to_response('rango / category.html',context_dict,context)

def add_category(request):context = RequestContext(request)

if request.method =='POST':form = CategoryForm(request.POST)

if form.is_valid():
    form.save(commit=True)
    return index(request)
else:
    print (form.errors)

else:form = CategoryForm()

return render_to_response('rango / add_category.html',{'form':form},context)

1回答

  • 0

    您需要向表单类添加 fieldsexclude 属性

    class CategoryForm(forms.ModelForm):
        class Meta:
            model = Category
            fields = ['name']
    

    好像你没有一个名为 Category 的模型,在这种情况下你应该从 forms.Form 继承

    class CategoryForm(forms.Form):
        name = forms.CharField(max_length=128, help_text="Please enter the category name.")
    

loading...

评论

暂时没有评论!