Django:从表单示例中保存到DB [英] Django: Saving to DB from form example
问题描述
看来,我很难从表单中找到一个关于从数据库保存数据的好的源码/教程。随着进步,我慢慢迷路了。我是Django的新手,请指导我。我收到错误
It seems I had difficulty finding a good source/tutorial about saving data to the DB from a form. And as it progresses, I am slowly getting lost. I am new to Django, and please guide me. I am getting error
分配前引用的局部变量'store'
local variable 'store' referenced before assignment
以下是我的相关代码,
models.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
views.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中添加的那个)
addfeedback.html(the one that calls add in views.py)
<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>
推荐答案
这是一个很好的 ModelForms教程。虽然它是为Django 1.3,所以它慢慢地变得过时了。
Here is a pretty good tutorial on ModelForms. Although it's for Django 1.3, so it's slowly becoming obsolete.
你应该只收到局部变量'store'在赋值前引用
直接访问表单提交URL时出错。如果表单已经发布到网址,则应分配存储。
You should only receive the local variable 'store' referenced before assignment
error when you access the form submission url directly. If a form has been posted to the url, store should be assigned.
您正在尝试基于模型创建表单,因此我将解释如何使用ModelForm。
You are trying to create a form based on a model, so I'll explain how you could use a ModelForm.
您的反馈模型有一个DateTimeField,您正在存储提交反馈的时间戳。您可以通过将 auto_now_add
设置为true来实现自动化
Your feedback model has a DateTimeField and you are storing the timestamp of when the feedback was submitted. You can automate this by setting auto_now_add
to true
date = models.DateTimeField("comment_date", auto_now_add=True)
然后创建一个 forms.py 在您的应用文件夹中使用以下
Then create a forms.py in your app folder with the following
from django.forms import ModelForm
from your_app.models import Feedback
...
class FeedbackForm(ModelForm):
class Meta:
model = Feedback
exclude = ('store',)
您的views.py应包含一个显示和处理提交表单的功能
Your views.py should contain one function that displays and processes the submitted form
from your_app.forms import FeedbackForm
...
def add(request, store_name):
form = FeedbackForm(request.POST or None)
if form.is_valid():
feedback = form.save(commit=False)
store = Store.objects.get(store_name=store_name)
feedback.store = store
feedback.save()
这篇关于Django:从表单示例中保存到DB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!