Html 我能';t通过表单向数据库添加数据

Html 我能';t通过表单向数据库添加数据,html,python-3.x,django,django-forms,Html,Python 3.x,Django,Django Forms,我一直在制作一个网站,人们可以在这个网站上发布对他们访问过的餐厅的评价。我希望用户通过表单发布数据,但我不能让表单页面像我想象的那样工作。更详细地说,表单中的数据不会添加到数据库中。请告诉我为什么这个系统不工作。我在管理页面和“Tabelog/list.html”的表单中找不到我键入的数据 名为“form”的函数试图在页面上显示表单并将数据保存到数据库中 models.py from django.db import models stars = [ (1,"☆")

我一直在制作一个网站,人们可以在这个网站上发布对他们访问过的餐厅的评价。我希望用户通过表单发布数据,但我不能让表单页面像我想象的那样工作。更详细地说,表单中的数据不会添加到数据库中。请告诉我为什么这个系统不工作。我在管理页面和“Tabelog/list.html”的表单中找不到我键入的数据

名为“form”的函数试图在页面上显示表单并将数据保存到数据库中

models.py

from django.db import models

stars = [
    (1,"☆"),
    (2,"☆☆"),
    (3,"☆☆☆"),
    (4,"☆☆☆☆"),
    (5,"☆☆☆☆☆")
]

# Create your models here.
class Tabelog(models.Model):
    store_name = models.CharField("店名",max_length = 124)
    evaluation = models.IntegerField("評価",choices = stars)
    comment = models.TextField("口コミ")

    def outline(self):
        return self.comment[:10]

    def __str__(self):
        return ("{},{},{}".format(self.store_name,self.evaluation,self.comment[:10]))
forms.py

from django import forms
from django.forms import ModelForm
from Tabelog.models import Tabelog

class CreateTabelogForm(forms.ModelForm):
    class Meta:
        model = Tabelog
        fields = "__all__"
url.py

from django.urls import path,include
from Tabelog import views

app_name = "Tabelog"

urlpatterns = [
    path("lp/", views.lp,name="lp"),
    path("list/",views.list,name="list"),
    path("detail/<str:store_name>",views.detail,name="detail"),
    path("form/",views.form,name="form")
]
base.html

<!DOCTYPE html>
<html lang="ja" dir="ltr">
  <head>
    <meta charset="utf-8">
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">
    <title>Tabelog</title>
  </head>
  <body>
    <div class="container">
      <header>
        Welcome to Tabelog!
      </header>
      <main>
        {% block content%}
        {% endblock %}
      </main>
      <footer>Thank you for providing information!</footer>
    </div>
  </body>
</html>

塔贝洛格
欢迎来到塔贝洛!
{%block content%}
{%endblock%}
感谢您提供的信息!
form.html

<!DOCTYPE html>
{% extends 'Tabelog/base.html' %}
{% block content %}
<form action="" method="post">
  {{ form.as_p }}
  {% csrf_token %}
  <button type="submit">SUBMIT!</button>
</form>
{% endblock %}

{%extends'Tabelog/base.html%}
{%block content%}
{{form.as_p}}
{%csrf_令牌%}
提交
{%endblock%}
list.html

<!DOCTYPE html>
{% extends 'Tabelog/base.html' %}
{% block content %}
{% for contents in info%}
<article class="store">
  <h1> <a href="{% url 'Tabelog:detail' contents.store_name %}">{{contents.store_name}}</a></h1>
  <h2>{{contents.get_stars_display}}</h2>
  <span>{{contents.outline}}</span>
</article>
{% endfor %}
{% endblock %}

{%extends'Tabelog/base.html%}
{%block content%}
{info%中的内容为%0}
{{contents.get_stars_display}
{{contents.outline}
{%endfor%}
{%endblock%}

我想这里有个打字错误。您已经使用了
request.method==“post”
,它应该
request.method==“post”
。老实说,该支票是多余的,所以您应该删除它

def form(request):
    if request.method == "GET":
        form = CreateTabelogForm()
        context = {
            "form":form
        }
        return render(request,"Tabelog/form.html",context)

    else:
        form = CreateTabelogForm(request.POST or None)
        if request.method == "post" and form.is_valid():
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
           # remove this code
该代码的重构版本:

def form(request):
    form = CreateTabelogForm(request.POST or None)
    if request.method == "POST":
        if form.is_valid():
           form.save()
           return redirect("Tabelog:list")
     context = {
        "form":form
     }
     return render(request,"Tabelog/form.html",context)

非常感谢你!我终于可以解决这个问题了!令人惊叹的。那么请考虑一下。谢谢
def form(request):
    form = CreateTabelogForm(request.POST or None)
    if request.method == "POST":
        if form.is_valid():
           form.save()
           return redirect("Tabelog:list")
     context = {
        "form":form
     }
     return render(request,"Tabelog/form.html",context)