【问题标题】:Django form not saving even after everything seems all right即使一切正常,Django表单也没有保存
【发布时间】:2020-10-04 04:45:15
【问题描述】:

我的看法: ''' 从 django.shortcuts 导入渲染 从 django.http 导入 HttpResponse 从 .models 导入帖子 从 .forms 导入 createPostForm

def showPosts(request):
    if request.method == "POST":
        crf = createPostForm(request.POST)

        crf.author = request.user
        crf.save()


    else:
        crf = createPostForm()
    context = {
        'post' : Post.objects.all(),
        'crf' : crf

    }
    return render(request, 'Content/FeedsPage.html', context)

'''

我的模型: '''

from django.db import models
from django.contrib.auth.models import User
class Post(models.Model):
    # image = models.ImageField(blank=True, null=True, upload_to='post_images/')
    # video = models.FileField(blank=True, null=True, upload_to='post_videos/')
    title = models.CharField(max_length=100)
    description = models.CharField(blank=True, max_length=1000)
    date_posted = models.DateTimeField(auto_now_add=True)
    author = models.ForeignKey(User, on_delete=models.CASCADE)

    def __str__(self):
        return self.title

'''

我的模板:

'''

<form enctype="multipart/form-data" novalidate method="post">
                    {%csrf_token%}
                    <div class="fieldWrapper">
                        {{crf.title.errors}}
                        {{crf.title}}
                    </div>
                    <div class="fieldWrapper">
                        {{crf.description.errors}}
                        {{crf.description}}
                    </div>
                    <button class="primaryButton" type="submit">submit</button>
                </form>

''' 我的表格:

'''

from django import forms
from .models import Post
class createPostForm(forms.ModelForm):

    title = forms.CharField(
        widget = forms.TextInput(attrs={
        'placeholder': 'Give a sweet title',
        'autocomplete' :'off'
        })
    )
    description = forms.CharField(widget=forms.TextInput(attrs={
        'placeholder': 'Please elaborate a little',
        'autocomplete' :'off'
        }))

    class Meta:
        model = Post
        fields = '__all__'

'''

我删除了 is_valid() 函数以查看正在发生的事情并明显显示它 '无法创建帖子,因为数据未验证'

请人帮忙

【问题讨论】:

    标签: django forms


    【解决方案1】:

    这个 save() 方法接受一个可选的 commit 关键字参数,它接受 True 或 False。如果您使用 commit=False 调用 save(),那么它将返回一个尚未保存到数据库的对象。在实际保存之前,这对于一些后期处理来说是很好的!所以,你的方法应该是这样的。

    def showPosts(request):
    if request.method == "POST":
        crf = createPostForm(request.POST)
    
        if crf.is_valid():
    
        form = crf.save(commit=False)
        form.author = request.user
        form.save()
    
    
    else:
        crf = createPostForm()
    context = {
        'post' : Post.objects.all(),
        'crf' : crf
    
    }
    return render(request, 'Content/FeedsPage.html', context)
    

    【讨论】:

      猜你喜欢
      • 2018-11-12
      • 1970-01-01
      • 1970-01-01
      • 2013-06-22
      • 2019-03-15
      • 1970-01-01
      • 1970-01-01
      • 2015-10-13
      • 2022-01-26
      相关资源
      最近更新 更多