I have a form created in mysite/new_player.html. It accepts 3 fields, user_name, real_name, and site_played that correspond to the Player table in the database.
<h1> New Player </h1>
{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}
<form action="/stakeme/new/" method="post">
{% csrf_token %}
User Name: <input type="text" name="user_name" id="user_name"/><br>
Real Name: <input type="text" name="real_name" id="real_name"/><br>
Site Played: <input type="text" name="site_played" id="site_played"/><br><br>
<input type="submit" value="New Player" />
</form>
I am stuck on how to add this to my mysite/views.py file. I have gone through the polls tutorial, but the only form that is used in the tutorial is a multiple choice “choice” of the “poll” and I can’t seem to adapt that to text fields.
def new_player(request):
return render_to_response('stakeme/new_player.html',
context_instance=RequestContext(request))
So as I understand it, I would need to create something like def add(request): return render_to_response('stakeme/new/'.. etc and add the POST data in here, but that’s where I am lost. I am not sure how to get the data into the database.
I am reading the Django docs, but I feel like I am just compounding something that I do not understand. If someone could point me in the right direction, I would really appreciate it.
Firstly, you don’t need to define a new view to process the form data. Also, you are creating your form directly in HTML – it’s possible to work this way (see later section of post) but it’s better (easier) to use the Django Forms library.
Using Django Forms
The documentation (v1.3 forms documentation) from the start up to and including ”Displaying a form using a template” explains the basics of using the forms library, so I’ll copy & paste liberally from there. I’ll also assume that you’re familiar with basic python constructs & have Django 1.3 installed. Without further ado, here’s my adhoc forms tutorial.
Start a new django project:
Add a new app:
Lets create our contact form (modified from example in Django forms doc). Create a file in side the
myapp/directory called calledforms.pyand put the following in it:Next, since you mentioned storing data from received contact forms in a database, we’ll add a model, Feedback, to track received contact forms. In your
models.pyfile, add the following:(You may notice this is very similar to the form we defined earlier; normally in a scenario like this, one would use Django model forms to create a form directly from a model, but we are building our forms by hand as a learning experience)
We also need to get Django to create the required table in our database for this Feedback model, so at the top of your
settings.pyinsert the following useful code:And change the
DATABASESsetting insettings.pyto the following to use asqlitedatabase:Finally, change the
INSTALLED_APPSsetting to the following to include our recently created applicationmyappin the list of installed applications formysite:Now run the
syncdbcommand to get Django to create the tables in your sqlite database (which, since it’s sqlite, will be created if it doesn’t exist yet):(Django will prompt you to create a superuser as well: you don’t have to create a superuser now since we don’t need it and you can use
django-admin.py createsuperuserto create one when you need it, but you can create now now if you like)Now we need a view to display the contact form, and a view to thank people for submitting it. In your
views.pyfile, add the following (modified slightly from Django forms docs):Now we need to map URLs to views. Open
mysite/urls.pyand make it look like the followingNow we need some templates to display the contact form & the thankyou page. Create a directory
mysite/templates/, create a filecontact.htmlinside it, and put the following in it:Also create a
thanks.htmlpage for the thank you page, and put the following in it:Next, we need to make sure Django can find our templates, so modify the
TEMPLATE_DIRSinmysite/settings.pysetting to the following:Now, (finally!), you can run the debug server and test that everything works:
Go to http://localhost:8080/ and try to enter some feedback. When you click Submit, it should put your entered details into the database & show the thank you page. You can check the details are entered into the database:
Into the shell, type:
(note that you need to press enter twice after entering the last line)
You should see the feedback entries you have created.
Creating forms manually in HTML
If you insist on doing this, you can access the form’s request variables directly in your view using the
request.POSTdictionary, and then instantiating a model of your object manually & calling save (like in thecontact()view function above).I would not recommend doing this, because you lose a whole bunch of nice features that Django Forms provides (CSRF protection, validation, etc).
Other Tutorials
Since the original form of this question asked for some tutorials: the official Django wiki has a page listing some tutorials, some of which deal with forms. Be aware that a lot of those tutorials are quite old (mostly from 2007-2009).