RemovedInDjango18Warning:在没有'字段的情况下创建ModelForm。属性或'排除'属性已弃用

时间:2015-02-03 18:36:42

标签: python django

我正在做一个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):

此外,访问我的实际localhost网站时,我也遇到以下错误:

ValueError at /articles/create/

The view article.views.create didn't return an HttpResponse object. It returned None instead.

这是我的forms.py文件:

from django import forms
from models import Article

class ArticleForm(forms.ModelForm):

    class Meta:
        model = Article 

这是我的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)

我不确定如何解决这个问题。我查看了Django文档但我无法找到解决问题的方法,所以任何帮助都会受到赞赏。

4 个答案:

答案 0 :(得分:91)

对于您的表单,这是一个警告,而不是错误,告诉您在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

或者将exclude添加到要排除的列表字段

直到1.8才需要这样做

https://docs.djangoproject.com/en/1.8/topics/forms/modelforms/#selecting-the-fields-to-use

至于您的观看错误,您的返回位于if语句:if request.POST:内,因此当收到请求时,不会返回任何内容。

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)

只需查看else块,以便它适用于正确的if语句。

答案 1 :(得分:7)

你只需......

from django import forms
from models import Article

class ArticleForm(forms.ModelForm):

    class Meta:
        model = Article
        exclude = ()

...来修复你的表格。您需要发布您的观看代码,以了解其中的内容。

答案 2 :(得分:5)

如果您使用fields = __all__作为Ngenator建议,并且如果它是一个可能必须在各种版本的Django下运行的项目,则需要以下条件:

if django.VERSION >= (1, 6):
    fields = '__all__'  # eliminate RemovedInDjango18Warning

否则您会收到错误django.core.exceptions.FieldError: Unknown field(s) (a, l, _) specified for CrispyTestModel,如下所示:https://travis-ci.org/maraujop/django-crispy-forms/jobs/56996180

答案 3 :(得分:2)

在您看来,如果请求不是POST,则不会返回任何内容。您应该从else语句开始向后移动一个缩进。