Django: Saving to DB from form example

2024/9/19 10:10:47

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>
Answer

Use django.forms for the job. Don't put data straight from POST to db. See the related documentation.

local variable 'store' referenced before assignment

The error is obvious – you're referencing store even if the request isn't POST.

https://en.xdnf.cn/q/72662.html

Related Q&A

eval(input()) in python 2to3

From the Python 2to3 doc:input:Converts input(prompt) to eval(input(prompt))I am currently trying to learn Python 3 after a few years working with Python 2. Can anybody please explain why the tool inse…

Post XML file using Python

Im new to Python and in need of some help. My aim is to send some XML with a post request to a URL, which is going to trigger a SMS being sent. I have a small XML document that I want to post to the UR…

Python TypeError: __init__() got multiple values for argument master

Trying to build a GUI in Python at the moment, and Im stuck at this part in particular. Every time I try to run my code it just throws the error TypeError: __init__() got multiple values for argument m…

How to suppress all warnings in window of executable file generated by pyinstaller

I have generated an executable file from a python file using pyinstaller. The program works how it is supposed to work but there is this warning message it appears in the window that I would like to hi…

Python requests gives me bad handshake error

Using Python requests like thisimport requests; requests.get(https://internal.site.no)gives me an error many have had;SSLError: ("bad handshake: Error([(SSL routines, SSL23_GET_SERVER_HELLO, sslv3…

PyCharm: Storing variables in memory to be able to run code from a checkpoint

Ive been searching everywhere for an answer to this but to no avail. I want to be able to run my code and have the variables stored in memory so that I can perhaps set a "checkpoint" which I …

Execute bash script from Python on Windows

I am trying to write a python script that will execute a bash script I have on my Windows machine. Up until now I have been using the Cygwin terminal so executing the bash script RunModels.scr has been…

Python regex convert youtube url to youtube video

Im making a regex so I can find youtube links (can be multiple) in a piece of HTML text posted by an user.Currently Im using the following regex to change http://www.youtube.com/watch?v=-JyZLS2IhkQ in…

Python / Kivy: conditional design in .kv file

Would an approach similar to the example below be possible in Kivy? The code posted obviously doesnt work, and again its only an example: I will need different layouts to be drawn depending on a certa…

z-axis scaling and limits in a 3-D scatter plot

I performed a Monte Carlo inversion of three parameters, and now Im trying to plot them in a 3-D figure using Matplotlib. One of those parameters (Mo) has a variability of values between 10^15 and 10^2…