HTML表单数据未保存在数据库中-Django [英] HTML form data not saved in database - django

查看:67
本文介绍了HTML表单数据未保存在数据库中-Django的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以从html获取数据,但不会将其保存到数据库,该怎么办?它可以正常工作,唯一的问题是它不会被保存

views.py

  def注释(request,newsId):cm = get_object_or_404(models.News,id = newsId)print("news =" + newsId)如果request.method =='POST':cm.comments_set.text = request.POST.get('comment_text')cm.comments_set.name = request.POST.get('comment_name')cm.save()返回HttpResponseRedirect(reverse('details',args =(cm.id,))) 

urls.py

  urlpatterns = [path('',views.test),path('details/< newsId>',views.details,name ="details"),path('comment/< newsId>',views.comment,name ="comment")] 

models.py

  class News(models.Model):标题= models.CharField(max_length = 300)作者= models.CharField(max_length = 100)日期= models.DateTimeField()描述= models.TextField()像= models.IntegerField(默认= 0)img = models.CharField(max_length = 100)def __str __():返回self.title类评论(models.Model):新闻= models.ForeignKey(新闻,on_delete = models.CASCADE)名称= models.CharField(max_length = 100)文字= models.TextField()def __str __():返回self.name 

html表单

 < form action ="{%url'comment'newsKey.id%}" method ="POST">{%csrf_token%}< textarea name ="comment_text" id ="comment_text_id" cols ="30" rows ="10" placeholder =在此处写您的评论"></textarea><输入type ="text" name ="coment_name" id ="comment_name_id" placeholder =输入全名"/>< button type ="submit" value ="comment_submit">SUBMMIT</button></form> 

解决方案

您在这里所做的是保存原始的 News 对象( not Comment),然后在 comments_set 上设置一些随机属性.

基本上,您正在拍摄空白:)

 #修复了此部分.news_article = get_object_or_404(models.News,id = newsId)评论= models.Comment(name = request.POST.get('comment_name',''),text = request.POST.get('comment_text',''),news = news_article)comment.save() 

注释集用于选择给定的新闻文章的所有相关注释.

请参见 django教程./p>

也可以使用 create 方法来简化上述操作:

  models.Comment.objects.create(name = request.POST ['comment_name'],text = request.POST ['comment_text'],news = news_article) 

此方法还保存到数据库中.

I can get data from html but it wont save to database what should i do ? it works correctly the only problem is that it wont be save

views.py

def comment(request , newsId):
cm = get_object_or_404(models.News , id= newsId)
print("news = " + newsId)

if request.method == 'POST' :
    cm.comments_set.text = request.POST.get('comment_text')
    cm.comments_set.name = request.POST.get('comment_name')
    cm.save()


return HttpResponseRedirect(reverse('details', args=(cm.id,)))

urls.py

urlpatterns = [
   path('', views.test),
   path('details/<newsId>', views.details, name="details"),
   path('comment/<newsId>' , views.comment ,name="comment")]

models.py

class News(models.Model):
    title = models.CharField(max_length=300)
    author = models.CharField(max_length=100)
    date = models.DateTimeField()
    description = models.TextField()
    like = models.IntegerField(default=0)
    img = models.CharField(max_length=100)

    def __str__(self):
        return self.title


class Comments(models.Model):
    news = models.ForeignKey(News, on_delete=models.CASCADE)
    name = models.CharField(max_length=100)
    text = models.TextField()

    def __str__(self):
        return self.name

html form

<form action="{% url 'comment' newsKey.id %}" method="POST">
     {% csrf_token %}
     <textarea name="comment_text" id="comment_text_id"  cols="30" rows="10" placeholder="Write your here comment here"></textarea>
     <input type="text" name="coment_name" id="comment_name_id"  placeholder="Type full name"/>
     <button type="submit" value="comment_submit"> SUBMMIT </button>
</form>

解决方案

What you're doing here is saving the original News object (not a Comment) after settings some random properties on the comments_set.

Basically you're shooting blanks :)

# fix this part. 
news_article = get_object_or_404(models.News, id=newsId)

comment = models.Comment(
    name=request.POST.get('comment_name' , ''), 
    text=request.POST.get('comment_text',''),
    news=news_article 
)

comment.save()

The comment_set is used to select all the related comments to the given News article.

See this django tutorial.

The above can also be shortened by using the create method like so:

models.Comment.objects.create(
    name=request.POST['comment_name'], 
    text=request.POST['comment_text'],
    news=news_article 
)

This method also saves to the database.

这篇关于HTML表单数据未保存在数据库中-Django的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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