草庐IT

python - RemovedInDjango18 警告 : Creating a ModelForm without either the 'fields' attribute or the 'exclude' attribute is deprecated

coder 2023-05-23 原文

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

另外,我在访问我的实际本地主机站点时也遇到了以下错误:

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 文档,但找不到解决问题的方法,因此不胜感激。

最佳答案

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

至于您的 View 的错误,您的返回是在 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)

只需删除 else block ,以便将其应用于正确的 if 语句。

关于python - RemovedInDjango18 警告 : Creating a ModelForm without either the 'fields' attribute or the 'exclude' attribute is deprecated,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28306288/

有关python - RemovedInDjango18 警告 : Creating a ModelForm without either the 'fields' attribute or the 'exclude' attribute is deprecated的更多相关文章

随机推荐