似乎我很难找到关于将数据从表单保存到数据库的良好资源/教程。随着它的进展,我慢慢地迷失了。我是 Django 新手,请指导我。我收到错误
赋值前引用的局部变量“store”
这是我的相关代码,
模型.py
from django.db import models
# Create your models here.
class Store(models.Model):
store_name = models.CharField(max_length=100)
def __unicode__(self):
return self.store_name
class Feedback(models.Model):
store = models.ForeignKey(Store)
username = models.CharField(max_length=100)
comment = models.CharField(max_length=1000)
date = models.DateTimeField("comment_date")
def __unicode__(self):
return self.username
视图.py
def add(request, store_name):
if request.method == "POST":
store = Store.objects.get(store_name=store_name)
saved_username = request.POST.get("username", "")
saved_feedback = request.POST.get("feedback", "")
feedback = Feedback(username=saved_username, comment=saved_feedback, date=timezone.now())
feedback.save()
return HttpResponseRedirect(reverse("view", args=(store.id,)))
addfeedback.html(在views.py中调用add的那个)
<html>
<head><title>Add Feedback</title>
<link rel="stylesheet" type="text/css" href={{ STATIC_URL }}styles.css>
</head>
<body>
<div class="form">
<form action="{% url add store.store_name %}" method="post">
{% csrf_token %}
<input type="text" name="username" size="20"><br />
<textarea name="feedback" cols="50" rows="10"></textarea><br />
<input type="submit" value="Add" />
</form>
</body>
</html>