简体   繁体   中英

django form can't post data?

views.py:

def sub_comment(request):
    if request.method == 'POST':
        form = CommentCreateForm(request.POST, request.FILES)
        content=request.POST.get('content')
        print ',==>', content
        topic_id=request.POST.get('topic_id')
        print ',--->', topic_id
        if form.is_valid():

        new_comment = Comment(content=content,
                             topic_id=topic_id,
                             created_by=request.user,
                             )
        new_comment.save()
        return HttpResponseRedirect('/topic/topic_detail/%s' % topic_id)
else:
    form = CommentCreateForm()
variables = RequestContext(request, {'form':form})
return render_to_response('topic/topic_detail.html', variables)

forms.py:

class CommentCreateForm(forms.ModelForm):

    class Meta:
        model = Comment
        fields = ['content', 'created_by',]

templates:

<form action="/topic/sub_comment/" method="post" enctype="multipart/form-data" name="comment">
{% csrf_token %}

<textarea name="content" rows="8" cols="50"></textarea>
<input type="hidden" name="topic_id" value="{{ topics.id }}">
<input type="submit" value="Add comments" />
</form>

There is no error, but comment's data can't save to my database, and it turn into http://127.0.0.1:8000/topic/sub_comment website. urls.py: url(r'^sub_comment/$', views.sub_comment, name="sub_comment"),

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM