django-main-thread中的异常:

时间:2019-10-22 10:58:59

标签: python django

从模型创建表单

我是django的新手,尝试对表格进行建模,但不幸的是,即使我也两次检查了我的代码,我还是检查了django文档,但仍未弄清楚我的问题,所以出现了一些奇怪的错误...

我访问了django文档,但还没有弄清楚!!!相当混乱!

https://docs.djangoproject.com/en/2.2/topics/forms/modelforms/

AppFive / forms.py

from django import forms
from AppFive.models import User
from django.forms import ModelForm

class NewUserForm(ModelForm):
    class Meta:
        model = User
        fields = '__all__ ' # <-- Mistake over here - This Line. SOLVED!

AppFive / models.py

from django.db import models

# Create your models here.
class User(models.Model):
    first_name = models.CharField(max_length=128)
    last_name = models.CharField(max_length=128)
    email = models.EmailField(max_length=264,unique=True)

AppFive / views.py

from django.shortcuts import render
# from django.http import HttpResponse
# from AppFive.models import User
# Create your views hereself.
from AppFive.forms import NewUserForm

def index(request):
    return render(request,'AppFive/index.html')

def users(request):
    form = NewUserForm()
    if request.method == "POST":
        form = NewUserForm(request.POST)

        if form.is_valid():
            form.save(commit=True)
            return index(request)
        else:
            print('ERROR : F O R M    I N V A L I D')
    return render(request,'AppFive/users.html',{'form':form})
Traceback (most recent call last):
  File "C:\Users\Khan\Miniconda3\envs\MyDjangoEnv\lib\threading.py", line 926, in _bootstrap_inner
    self.run()
  File "C:\Users\Khan\Miniconda3\envs\MyDjangoEnv\lib\threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "C:\Users\Khan\Miniconda3\envs\MyDjangoEnv\lib\site-packages\django\utils\autoreload.py", line 54, in wrapper
    fn(*args, **kwargs)
  File "C:\Users\Khan\Miniconda3\envs\MyDjangoEnv\lib\site-packages\django\core\management\commands\runserver.py", line 117, in inner_run
    self.check(display_num_errors=True)
  File "C:\Users\Khan\Miniconda3\envs\MyDjangoEnv\lib\site-packages\django\core\management\base.py", line 390, in check
    include_deployment_checks=include_deployment_checks,
  File "C:\Users\Khan\Miniconda3\envs\MyDjangoEnv\lib\site-packages\django\core\management\base.py", line 377, in _run_checks
    return checks.run_checks(**kwargs)
  File "C:\Users\Khan\Miniconda3\envs\MyDjangoEnv\lib\site-packages\django\core\checks\registry.py", line 72, in run_checks
    new_errors = check(app_configs=app_configs)
  File "C:\Users\Khan\Miniconda3\envs\MyDjangoEnv\lib\site-packages\django\core\checks\urls.py", line 40, in check_url_namespaces_unique
    all_namespaces = _load_all_namespaces(resolver)
  File "C:\Users\Khan\Miniconda3\envs\MyDjangoEnv\lib\site-packages\django\core\checks\urls.py", line 57, in _load_all_namespaces
    url_patterns = getattr(resolver, 'url_patterns', [])
  File "C:\Users\Khan\Miniconda3\envs\MyDjangoEnv\lib\site-packages\django\utils\functional.py", line 80, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:\Users\Khan\Miniconda3\envs\MyDjangoEnv\lib\site-packages\django\urls\resolvers.py", line 579, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "C:\Users\Khan\Miniconda3\envs\MyDjangoEnv\lib\site-packages\django\utils\functional.py", line 80, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:\Users\Khan\Miniconda3\envs\MyDjangoEnv\lib\site-packages\django\urls\resolvers.py", line 572, in urlconf_module
    return import_module(self.urlconf_name)
  File "C:\Users\Khan\Miniconda3\envs\MyDjangoEnv\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 728, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "C:\Users\Khan\Desktop\Python Bootcamp\PB2\ProFive\ProFive\urls.py", line 18, in <module>
    from AppFive import views
  File "C:\Users\Khan\Desktop\Python Bootcamp\PB2\ProFive\AppFive\views.py", line 5, in <module>
    from AppFive.forms import NewUserForm
  File "C:\Users\Khan\Desktop\Python Bootcamp\PB2\ProFive\AppFive\forms.py", line 5, in <module>
    class NewUserForm(ModelForm):
  File "C:\Users\Khan\Miniconda3\envs\MyDjangoEnv\lib\site-packages\django\forms\models.py", line 235, in __new__
    raise TypeError(msg)
TypeError: NewUserForm.Meta.fields cannot be a string. Did you mean to type: ('__all__ ',)?

python 3.7.4

django(2,2,3,'final',0)

2 个答案:

答案 0 :(得分:0)

如caot所述,您需要提供正确答案的代码。无论如何,让我猜一猜。

当尝试从模型生成表单时,需要指定模型以及模型中的字段(您希望显示/捕获)。

应该为这些字段分配一个(严格地)由字段名称组成的列表,每个列表都是一个字符串。如果要包括所有字段,则可以指定“ 全部”作为字符串(是,字符串;不是列表)。

我想,您在指定 all 时犯了一个错误,或者您将字段包含为字符串(fields ='pub_date'或fields ='pub_date,标题')

如果我的回答不能解决您的问题,请显示完整代码。

欢呼

答案 1 :(得分:0)

fields = '__all__ '

字符串中有多余的空格。删除它!