Search code examples
pythondjangoerrno

comment django errno 22


Hi i try add comment to my django blog procject and i get OSError: [Errno 22] Invalid argument: "C:\Users\marci\PycharmProjects\08.04\blog\templates\"

so my urls

  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


Solution

  • You should be using render instead of render_to_response. You should also include article in the template context if you use it. Deindent the template and contect lines, so that the view works for invalid post requests.

    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)