Reputation: 37
I have been working on making a website in which people can post the evaluation of the restaurant they visited. I want users to post data through a form, but I can't make a form page work as I imagined. To speak the detail, the data put in the form isn't added to the database. Please tell me why the system doesn't work. I can't find the data I typed in the form on admin page and "Tabelog/list.html".
The function named "form" is the one trying to show the form on the page and save data to the database.
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__"
urls.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")
]
views.py
from django.shortcuts import render
from Tabelog.models import Tabelog
from Tabelog.forms import CreateTabelogForm
# Create your views here.
def lp(request):
return render(request,"Tabelog/lp.html")
def list(request):
info = Tabelog.objects.all()
context = {
"info":info,
}
return render(request,"Tabelog/list.html",context)
def detail(request,store_name):
detail = Tabelog.objects.get(store_name = store_name)
context = {
"detail":detail,
}
return render(request,"Tabelog/detail.html",context)
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():
form.save()
return redirect("Tabelog:list")
else:
form = CreateTabelogForm()
context = {
"form":form
}
return render(request,"Tabelog/form.html",context)
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>
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 %}
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 %}
Upvotes: 0
Views: 52
Reputation: 51938
I think there is a typo here. You have used request.method=="post"
, it should request.method == "POST"
. To be honest, that check is reduandent, so you should remove it.
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
A refactored version of that 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)
Upvotes: 1