Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
237 views
in Technique[技术] by (71.8m points)

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...