【问题标题】:How to add data to specific user and show in specific page?如何将数据添加到特定用户并显示在特定页面中?
【发布时间】:2020-05-14 14:11:41
【问题描述】:

我想根据用户特定的登录添加数据。我被困在如何做到这一点,这是我下面的代码。

views.py [我添加数据的实际代码]

def addintro(request):
        form = AboutForm()
        if request.method == 'POST':
            form = AboutForm(request.POST,request.FILES)
            if form.is_valid():
                form.save()
                return redirect('addintro')

        total=About.objects.all()
        context = {
            'form':form,
            'total':total,
        }
        return render(request, 'Backend/aboutme/Intro/add-intro.html', context)

我尝试了这个方法,它有效但是我想在这个代码中传递我的表单。我可以在这个代码中传递我的表单吗?

def addintro(request):
    if request.method == "POST":
        data = request.POST['data']
        new = About(description=data, author=request.user)
        new.save()
        return render(request, 'Backend/aboutme/Intro/add-intro.html')
    else:
        return render(request, 'Backend/aboutme/Intro/add-intro.html')

forms.py

class AboutForm(forms.ModelForm):
    class Meta:
        model = About
        widgets = {
            'description': SummernoteWidget(attrs={'summernote': {'width': '100%', 'height': '400px'}}),
        } 
        fields = '__all__'

models.py

class About(models.Model):
    author = models.ForeignKey(User, on_delete=models.CASCADE,null=True,blank=True,related_name='+')
    description=models.TextField("Write About You",null=True,blank=True)
    image=models.ImageField(null=True,blank=True)

【问题讨论】:

    标签: django python-3.x django-models django-forms django-views


    【解决方案1】:

    我猜你想问什么,我假设根据你的代码,你的视图没有向你显示表单。

    1) 您没有向render 传递任何数据,因此什么也不显示。 2) 当它收到不同于 POST 的请求时,不声明表单。 3) 表单名为 AboutForm 而不是 About,因为这是您的目标模型。

    包含form.is_valid() 检查总是好的。

    def addintro(request):
        if request.method == "POST":
            newform = Aboutform(request.POST)
            if form.is_valid():
                new = newform.save(commit=False)
                new.author = request.user
                new.save()
            # use redirect instead of render after form saved
            return redirect('Backend/aboutme/Intro/add-intro.html')
        else:
            new = AboutForm() #if request is not POST, pass empty form
        context = {
            'form' = form
            'content' = yourcontent #if your view want to pass extra content
        }
        return render(request, 'Backend/aboutme/Intro/add-intro.html', context)
    

    【讨论】:

    • 感谢您的友好回复。它现在正在工作。我现在可以成功看到登录的用户特定页面和特定数据。非常感谢。
    • @Pankajkushwaha Grad 知道它有效,考虑标记为已回答
    • 我在 django 项目中需要你的帮助,你能不能给一些时间来完成它。如果你有空给我留言link
    【解决方案2】:

    上述问题现已解决。这是我的工作代码

    def addintro(request):
            if request.method == "POST":
                
    
        newform = AboutForm(request.POST,request.FILES)
                    if newform.is_valid():
                        new = newform.save(commit=False)
                        new.author = request.user
                        new.save()
                    return redirect('addintro')
                else:
                    new = AboutForm()
            
                log_user=request.user
                total=About.objects.filter(author=log_user)
                context = {'new':new,'total':total}
                return render(request, 'Backend/aboutme/Intro/add-intro.html',context)*
    

    【讨论】:

      猜你喜欢
      • 2016-05-16
      • 2018-02-26
      • 2020-08-20
      • 2021-03-22
      • 1970-01-01
      • 2021-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多