RemovedInDjango18警告:不建议使用“fields”属性或“exclude”属性的ModelForm已被弃用 [英] RemovedInDjango18Warning: Creating a ModelForm without either the 'fields' attribute or the 'exclude' attribute is deprecated

查看:191
本文介绍了RemovedInDjango18警告:不建议使用“fields”属性或“exclude”属性的ModelForm已被弃用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个Django项目,当我尝试访问127.0.0.1:8000/articles/create时,我的Ubuntu终端出现以下错误:

  / home /(我的名字)/django_test/article/forms.py:4:RemovedInDjango18警告:创建一个没有fields属性或exclude属性的ModelForm已被弃用 - 表单ArticleForm需要更新
class ArticleForm(forms.ModelForm):

此外,我还得到访问我的实际本地站点时出现以下错误:

  / article / create / 

的ValueError article.views.create没有返回HttpResponse对象。它返回无。

这是我的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文章
from django.http import HttpResponse
from从django.http中导入ArticleForm
import httpResponseRedirect
from django.core.context_processors import csrf

#import pdb; pdb.set_trace()
#在这里创建您的观点。

def article(request):
language ='en-us'
session_language ='en-us'

如果'lang'在请求中.COOKIES:
language = request.COOKIES ['lang']
如果request.session中的lang:
session_language = request.session ['lang']

return render_to_response('articles.html',
{'articles':
Article.objects.all(),'language':language,
'session_language':session_language})

def article(request,art​​icle_id = 1):
return render_to_response('article.html',{'article':
Article.objects.get(id = article_id)} )

def language(request,language ='en-us'):
response = HttpResponse(将语言设置为%s%
语言)

response.set_cookie('lang',language)
response.session ['lang'] = language

返回响应

def create(request):
如果request.POST:
form = ArticleForm(request.POST)
如果form.is_valid():
form.save()

返回HttpResponseRedirect('/ articles / all')

else:
form = ArticleForm()

args = {}
args.update(csrf(request))

args ['form'] = form

return render_to_response('create_article.html',args)

我不知道如何解决这个问题。我查看了Django文档,但是找不到我的问题的解决方案,所以任何帮助将不胜感激。

解决方案

您的表单,这是一个警告,而不是错误,告诉您在django 1.8中,您将需要将您的表单更改为

 从django导入表单
从模型导入文章

类ArticleForm(forms.ModelForm):

类Meta:
模型=文章
fields ='__all__'#或您要包含在表单中的字段列表

或添加一个排除以列出字段以排除



哪些不需要,直到1.8



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



至于错误与你的意见,你的r如果语句:如果request.POST:,那么如果收到获取请求,则没有返回任何内容

  def create(request):
if request.POST:
form = ArticleForm(request。 POST)
如果form.is_valid():
form.save()

返回HttpResponseRedirect('/ articles / all')

其他:
form = ArticleForm()

args = {}
args.update(csrf(request))

args ['form'] = form

返回render_to_response('create_article.html',args)

else 块,以便如果语句适用于正确的


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:

/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:

from django import forms
from models import Article

class ArticleForm(forms.ModelForm):

    class Meta:
        model = Article 

And here is my views.py file:

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.

解决方案

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

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

Which wasn't required up till 1.8

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.

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.

这篇关于RemovedInDjango18警告:不建议使用“fields”属性或“exclude”属性的ModelForm已被弃用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆