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
local variable 'store' referenced before assignment
Here are my relevant codes,
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_nameclass 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(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>