Python Django 1.10中args的正确语法是什么?
我开始研究Django1.10,但它使用了1.6上的示例。这就是我在新版本中遇到语法问题的原因 这是我的职责:Python Django 1.10中args的正确语法是什么?,python,django,Python,Django,我开始研究Django1.10,但它使用了1.6上的示例。这就是我在新版本中遇到语法问题的原因 这是我的职责: def article(request, article_id=1): comment_form = CommentForm @csrf_protect args = {} args['article'] = Article.objects.get(id=article_id) args['comments'] = Comments.object
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文档中找不到任何解释。CSRF Protect默认为打开状态,如果您想使用装饰器,需要将其放在类似的方法之前 您的
CommentForm
是表单中的一个对象。py(我想)您需要这样称呼他CommentForm()
但是你可以做得更简单,Django用相关的名字创建一个dict,例如:template.html中的{{article}
,wiews.py中的对象/变量的名字a
(who isComments.objects.filter(Comments\u artile\u id=article\u id)
)
@csrf\u protect
是一种安全保护。将其置于方法定义之上,以便工作。
另外,必须像方法体的其余部分一样缩进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)
尝试将
@csrf\u protect
置于函数上方。你是对的,这是一个错误。感谢you@AlexeyG欢迎来到StackOverflow!如果您的问题已经解决,请选择一个答案,将其标记为已接受,并对任何您认为有用的答案进行投票。这有助于后来知道哪些答案最有帮助的人,也会奖励那些不辞辛劳地帮助你的人。
@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})
@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)