python - 小白django提交数据后,没有存储到数据库(查阅资料并没有发现问题)

查看:196
本文介绍了python - 小白django提交数据后,没有存储到数据库(查阅资料并没有发现问题)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

views.py

#提交评论
def comment_post(request):
    try:
        if request.method == 'POST':
            comment_form = CommentForm(request.POST)
            print(comment_form)
            print(comment_form.is_valid())
        if comment_form.is_vaild():#验证表单
            #获取表单信息
            comment = Comment.objects.create(username=comment_form.cleaned_data["author"],
                                             email=comment_form.cleaned_data["email"],
                                             url=comment_form.cleaned_data["url"],
                                             content=comment_form.cleaned_data["comment"],
                                             article_id=comment_form.cleaned_data["article"],
                                             user=request.user if request.user.is_authenticated() else None)
            print('comment:',comment)
            comment.save()
        else:
            return render(request,'failure.html',{'reason':comment_form.errors})
    except Exception as e:
        logger.error(e)
    return redirect(request.META['HTTP_REFERER'])#HTTP_REFERE是header的一部分,浏览器向服务器发送请求的时候,告诉服务器我是从哪里过来的

urls.py

from django.conf.urls import url
from blog_project.views import *

urlpatterns = [
    url(r'^$',index,name='index'),
    url(r'^archive/$',archive,name='archive'),
    url(r'^article/$',article,name='article'),
    url(r'^comment/post/$',comment_post,name='comment_post'),
    url(r'^logout$',do_logout,name="logout"),
    url(r'^reg',do_reg,name='reg'),
    url(r'^login',do_login,name='login'),
    url(r'^category/$',category,name='category')
]

forms.py

class CommentForm(forms.Form):
    '''
    评论表单
    '''
    author = forms.CharField(widget=forms.TextInput(attrs={"id":"author","class":"comment_input",
                                                            "required":"required","size":"25","tabindex":"1"}),
                            max_length=50,error_messages={"required":"username不能为空",})
    email = forms.EmailField(widget=forms.TextInput(attrs={"id":"email","type":"email","class":"comment_input","required":"required","size":"25","tabindex":"2"}),
                            max_length=50,error_messages={"required":"email不能为空",})
    url = forms.URLField(widget=forms.TextInput(attrs={"id":"url","type":"url","class":"comment_input","size":"25","tabindex":"3"}),
                                               max_length=100,required=False)
    comment = forms.CharField(widget=forms.Textarea(attrs={"id":"comment","class":"message_input",
                                                            "required":"required","cols":"25",
                                                            "rows":"5","tabindex":"4"}),
                                                    error_messages={"required":"评论不能为空",})
    article = forms.CharField(widget=forms.HiddenInput())

article.html

            <form action="{% url 'comment_post' %}" method="post">
                {% csrf_token %}
                <p>{{comment_form.author}}
                    <label for="author">Name<span style="color:red;">*</span></label>
                </p>
                <p>{{comment_form.email}}
                    <label for="email">Email(Will NOT be published)<span style="color:red;">*</span></label>
                </p>
                <p>{{comment_form.url}}
                    <label for="url">URL</label>
                </p>
                <p>
                    {{comment_form.comment}}
                </p>
                <p>
                    {{comment_form.article}}
                    <input name="submit" type="submit" id="submit" tabindex="5" value="确认" class="button" />
                </p>
            </form>

这个问题已被关闭,原因:问题已解决 - 问题已解决,且对他人无借鉴意义

解决方案

一步一步排查,1表单是否提交数据到服务端,2把服务端的sql语句打印出来直接在数据库里面执行看看是否有错误。

这篇关于python - 小白django提交数据后,没有存储到数据库(查阅资料并没有发现问题)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆