2017-02-23 6 views
-4

私はDjango 1.10を勉強し始めましたが、1.6の例を使用しています。そのため、私は新しいバージョンの構文に問題があります。Django 1.10のargsの正しい構文は何ですか?

これは私の関数である:

def article(request, article_id=1): 
    comment_form = CommentForm 
    @csrf_protect 
    args = {} 
    args['article'] = Article.objects.get(id=article_id) 
    args['comments'] = Comments.objects.filter(comments_artile_id=article_id) 
    args['form'] = comment_form 
return render (request, 'articles.html', args) 

そして、私のトレースバック:

File "/home/goofy/djangoenv/bin/firstapp/article/views.py", line 30 

args = {}  
    ^
SyntaxError: invalid syntax 

私はいずれかを見つけることができないので、右の構文またはところ私は答えを見つけることができているものを私に見せて下さいDjango Docsの説明。

+1

「@ csrf_protect」を関数の上に配置してください。 – flowfree

+0

あなたは正しいです、間違いでした。ありがとうございました –

+1

@AlexeyGようこそStackOverflow!あなたの問題が解決された場合は、あなたが助けてくれたものを受諾してアップヴォートする答えを選択してください。これは、後に来る人々がどの回答が最も役に立つかを知るのに役立ちます。また、あなたを助けるために道を離れた人にも報酬を与えます。 –

答えて

0

CSRF Protectはデフォルトでオンになっています。デコレータを使用する場合は、documentation sayのような方法の前に置く必要があります。

あなたCommentFormは、あなたがそのCommentForm()

@csrf_protect 
def article(request, article_id=1): 
    comment_form = CommentForm() 
    args = {} 
    args['article'] = Article.objects.get(id=article_id) 
    args['comments'] = Comments.objects.filter(comments_artile_id=article_id) 
    args['form'] = comment_form 
    return render (request, 'articles.html', args) 

のように彼を呼び出す必要がし​​かし、あなたはそれが簡単に、Djangoは、関連する名前の例で辞書を作成して行うことができます(私は仮定)あなたのforms.py内のオブジェクトです。 template.htmlには{{ article }}、wiews.pyにはオブジェクト/変数の名前a(誰がComments.objects.filter(comments_artile_id=article_id))が含まれています。

@csrf_protect 
def article(request, article_id=1): 
    form = CommentForm() 
    a = Article.objects.get(id=article_id) 
    c = Comments.objects.filter(comments_artile_id=article_id) 
    return render (request, 'articles.html', { 
       'article': a, 
       'comments': c, 
       'comment_form': form}) 
1

@csrf_protectは、python decoratorである。作業のためにメソッド定義の上に置きます。 また、return行は、メソッド本体の残りの部分と同様にインデントしなければなりません。

@csrf_protect 
def article(request, article_id=1): 
    comment_form = CommentForm() 
    args = {} 
    args['article'] = Article.objects.get(id=article_id) 
    args['comments'] = Comments.objects.filter(comments_artile_id=article_id) 
    args['form'] = comment_form 
    return render (request, 'articles.html', args) 
関連する問題