为什么从我的表单输入不写到Django的sqlite3数据库中

问题描述 投票:0回答:3

我想在通过表单添加一些输入后更新列表,但是我看不到更新后的列表。我在列表中看到了现有项目,但是当我添加新项目时,它没有出现在列表中。我可以使用管理面板手动添加它,并在列表中(完全不同的路径)查看它,但不能使用我创建的用于输入和更新列表的表单。我能够查询我的数据库,并且表单中的输入没有写入数据库,这就是为什么它不显示任何更改的原因。下面是我的代码

models.py

class BlogPost(models.Model):
    notes = models.CharField(max_length = 1000000000000000000000000000)
    date = models.DateTimeField(auto_now_add=True)
    done = models.BooleanField(default=False)

    def __str__(self):
        return self.notes

form.py

from blog.models import BlogPost

class BlogForm(forms.ModelForm):
    class Meta:
        model = BlogPost
        fields = ['notes', 'done',]

views.py

from django.shortcuts import render,redirect
from django.http import HttpResponse,HttpResponseRedirect,HttpRequest
from blog.models import BlogPost
from blog.form import BlogForm


def home(request):
    context = {
            'welcome_text': 'Welcome to the home page. View some more stuff soon'
            }
    return render(request,'home.html', context)

def blogpost(request):
    if request.method == "POST":
        form = BlogForm(request.POST)
        if form.is_valid():
            if  form.save():
                message.success(request, "the task was added")


        return redirect('blogpost')
    else:

        all_blogs = BlogPost.objects.all

        return render(request, 'blog.html',{'the_blogs': all_blogs } )

blog.html

{%extends 'base.html' %}


{% block title%}
        <title> Blog  </title>
{% endblock title%}

{%block content %}
        <div class="container">
         <br>
         {%for message in messages%}
                {{message}}
         {% endfor %}
         <form method = 'POST'>
                {% csrf_token %}

                <div class="form-group">
                        <input type="text" class="form-control" name = 'blog'  placeholder = 'new blog' >
                </div>

                <button type="submit" class="btn btn-primary">Add Blog</button>
        </form>
        <br>
                <table class="table table-hover table-dark">

                        <thead>
                                <tr>
                                        <th scope="col">Blog </th>
                                        <th scope="col">Done</th>
                                        <th scope="col">Date</th>
                                        <th scope="col">Edit</th>
                                        <th scope="col">Delete</th>
                                </tr>
                        </thead>
                        <tbody>
                        {% for item in the_blogs %}
                                {% if item.done %}
                                        <tr class="table-success">
                                                <td >{{item.notes}}</td>
                                                <td >Not-Completed</td>
                                                <td>{{item.date}}</td>
                                                <td>edit</td>
                                                <td>delete</td>
                                        </tr>
                                {% endif %}

                        {% endfor %}
                        </tbody>
                </table>
        </div>
{%endblock content%}

如果您需要更多有关此的信息,请点击此处链接到我的GitHub存储库,其中包含更多代码。https://github.com/wfidelis/Django-App

django python-3.x sqlite
3个回答
0
投票

您必须更正代码缩进和get调用部分,将表单传递给上下文对象,并在模板上使用双大括号将其调用,还应在模板上添加一个action属性。

def blogpost(request):
    all_blogs = BlogPost.objects.all()
    if request.method == "POST":
        form = BlogForm(request.POST)
        if form.is_valid():
            if  form.save():
                message.success(request, "the task was added")
        return redirect('blogpost')
    else:
        form = BlogForm()
    return render(request, 'blog.html',{'form': form, 'the_blogs': all_blogs } )


<form method='POST' action="">{% csrf_token %}
 {{ form.as_p }}
 <button type="submit" class="btn btn-primary">Add Blog</button>
<form/>

0
投票

[添加博客时,请不要重定向,请尝试使用与此处相同的方法来呈现具有新列表的页面:

  all_blogs = BlogPost.objects.all
  return render(request, 'blog.html',{'the_blogs': all_blogs } )

或尝试将以JSON格式创建的新对象返回到前端(作为POST请求的响应),前端会将其通过jQuery或JS添加到HTML中


-1
投票

您可能想将form.py重命名为forms.py

© www.soinside.com 2019 - 2024. All rights reserved.