评论django errno 22

时间:2018-04-10 19:19:52

标签: python django errno

您好我尝试添加评论到我的django博客procject,我得到OSError:[Errno 22]无效的参数:“C:\ Users \ marci \ PycharmProjects \ 08.04 \ blog \ templates \”

所以我的网址

  path('<int:a_id>/addcomment', views.addcomment, name='addcomment'),

views.py

def addcomment(request, a_id):
article = get_object_or_404(Articles,id=a_id)

if request.method == 'POST':
    form = CommentForm(request.POST)
    if form.is_valid():
        comment = form.save(commit=False)
        comment.article = article
        comment.save()

        return HttpResponseRedirect('/article/%s' % a_id)
else:
    form = CommentForm()
    template = 'addcomment.html'
    context = {'form': form}

return render_to_response(request,template,context)

addcomment.html

{% extends 'main.html' %}

{% block article %}

<form action="/article/{{ article.id }}/addcomment/" method="post" class="form-horizontal well">{% csrf_token %}
    {{ form.as_p }}
    <input type="submit" class="btn btn-inverse" name="submit" value="Dodaj komentarz" />
</form>
{% endblock %}

THX

1 个答案:

答案 0 :(得分:0)

您应该使用render代替render_to_response。如果您使用它,还应在模板上下文中包含article。取消templatecontect行,以便该视图适用于无效的帖子请求。

def addcomment(request, a_id):
    article = get_object_or_404(Articles,id=a_id)

    if request.method == 'POST':
        ...
    else:
        form = CommentForm()
    template = 'addcomment.html'
    context = {'form': form, 'article': article}

    return render(request, template, context)