Python RemovedInDjango18Warning: 创建一个没有“fields”属性或“exclude”属性的 ModelForm 已被弃用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28306288/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
RemovedInDjango18Warning: Creating a ModelForm without either the 'fields' attribute or the 'exclude' attribute is deprecated
提问by brown1001
I am doing a Django project and when I tried to access 127.0.0.1:8000/articles/create, I got the following error in my Ubuntu terminal:
我正在做一个 Django 项目,当我尝试访问 127.0.0.1:8000/articles/create 时,我的 Ubuntu 终端出现以下错误:
/home/(my name)/django_test/article/forms.py:4: RemovedInDjango18Warning: Creating a ModelForm without either the 'fields' attribute or the 'exclude' attribute is deprecated - form ArticleForm needs updating
class ArticleForm(forms.ModelForm):
In addition, I also got the following error when visiting my actual localhost site:
此外,在访问我的实际本地主机站点时,我还收到以下错误:
ValueError at /articles/create/
The view article.views.create didn't return an HttpResponse object. It returned None instead.
Here is my forms.py file:
这是我的 forms.py 文件:
from django import forms
from models import Article
class ArticleForm(forms.ModelForm):
class Meta:
model = Article
And here is my views.py file:
这是我的 views.py 文件:
from django.shortcuts import render_to_response
from article.models import Article
from django.http import HttpResponse
from forms import ArticleForm
from django.http import HttpResponseRedirect
from django.core.context_processors import csrf
#import pdb; pdb.set_trace()
# Create your views here.
def articles(request):
language = 'en-us'
session_language = 'en-us'
if 'lang' in request.COOKIES:
language = request.COOKIES['lang']
if 'lang' in request.session:
session_language = request.session['lang']
return render_to_response('articles.html',
{'articles':
Article.objects.all(), 'language' : language,
'session_language' : session_language})
def article(request, article_id=1):
return render_to_response('article.html', {'article':
Article.objects.get(id=article_id) })
def language(request, language='en-us'):
response = HttpResponse("setting language to %s" %
language)
response.set_cookie('lang', language)
response.session['lang'] = language
return response
def create(request):
if request.POST:
form = ArticleForm(request.POST)
if form.is_valid():
form.save()
return HttpResponseRedirect('/articles/all')
else:
form = ArticleForm()
args = {}
args.update(csrf(request))
args['form'] = form
return render_to_response('create_article.html', args)
I'm not sure how to fix this problem. I looked at the Django documentation but I couldn't find a solution to my problem so any help would be appreciated.
我不知道如何解决这个问题。我查看了 Django 文档,但找不到解决我的问题的方法,因此将不胜感激。
采纳答案by Ngenator
For your form, it's a warning, not an error, telling you that in django 1.8, you will need to change your form to
对于您的表单,这是一个警告,而不是错误,告诉您在 django 1.8 中,您需要将表单更改为
from django import forms
from models import Article
class ArticleForm(forms.ModelForm):
class Meta:
model = Article
fields = '__all__' # Or a list of the fields that you want to include in your form
Or add an exclude
to list fields to exclude instead
或者添加一个exclude
以列出要排除的字段
Which wasn't required up till 1.8
直到 1.8 才需要
https://docs.djangoproject.com/en/1.8/topics/forms/modelforms/#selecting-the-fields-to-use
https://docs.djangoproject.com/en/1.8/topics/forms/modelforms/#selecting-the-fields-to-use
As for the error with your views, your return is inside of an if
statement: if request.POST:
so when it receives a get request, nothing is returned.
至于您的视图错误,您的返回在if
语句中:if request.POST:
因此,当它收到 get 请求时,不会返回任何内容。
def create(request):
if request.POST:
form = ArticleForm(request.POST)
if form.is_valid():
form.save()
return HttpResponseRedirect('/articles/all')
else:
form = ArticleForm()
args = {}
args.update(csrf(request))
args['form'] = form
return render_to_response('create_article.html', args)
Just dedent the else
block so that it's applying to the correct if
statement.
只需删除else
块,以便它应用于正确的if
语句。
回答by Brandon
You just need...
您只需要...
from django import forms
from models import Article
class ArticleForm(forms.ModelForm):
class Meta:
model = Article
exclude = ()
...to fix your form. You'll need to post your view code to see what's up with that.
...修复你的表格。您需要发布您的视图代码以查看发生了什么。
回答by Daniel Roseman
In your view, you don't return anything if the request is not a POST. You should move everything from the else
statement onwards back one indent.
在您看来,如果请求不是 POST,则不会返回任何内容。您应该将else
语句中的所有内容向后缩进一个缩进。
回答by jcomeau_ictx
if you use fields = __all__
as Ngenator suggested, and if it's a project that might have to run under various versions of Django, the following conditional will be needed:
如果您fields = __all__
按照 Ngenator 的建议使用,并且它是一个可能必须在各种版本的 Django 下运行的项目,则需要以下条件:
if django.VERSION >= (1, 6):
fields = '__all__' # eliminate RemovedInDjango18Warning
otherwise you get the error django.core.exceptions.FieldError: Unknown field(s) (a, l, _) specified for CrispyTestModel
, as seen here: https://travis-ci.org/maraujop/django-crispy-forms/jobs/56996180
否则你会得到错误django.core.exceptions.FieldError: Unknown field(s) (a, l, _) specified for CrispyTestModel
,如下所示:https: //travis-ci.org/maraujop/django-crispy-forms/jobs/56996180