Boston Python Workshop/Saturday/Web app project: Difference between revisions

Line 11:
This page should say what you should actually expect to know. It is okay that you don't understand everything you are typing in. After a lot more learning, you will be able to. The first time, though, it's okay if you don't. Will and Katie have feedback for this page.
 
Free knolwgdee like this doesn't just help, it promote democracy. Thank you.
== Writing your first Django app, part 1 ==
 
Let’s learn by example.
 
This tutorial walks you through the creation of a basic poll application.
 
It’ll consist of two parts:
 
* A public site that lets people view polls and vote in them.
* An admin site that lets you add, change and delete polls.
 
=== Switch to the right directory ===
 
* In a terminal (or GitBash), get into the django_projects directory we created in the Friday setup portion of the tutorial. You can do that by typing this into your terminal:
 
cd Desktop
cd django_projects
 
In the Friday setup portion of the workshop, you already saw how to use the ''django-admin.py'' command to start a project. The workshop coordinators already created a project, and you already ''fork''ed it on Github. So now, you'll clone that to your computer.
 
* Go to http://github.com/
* Find your clone of workshop_mysite. Find the '''SSH''' URL for it, and copy that to the clipboard.
* In the terminal, type: <tt>git clone</tt> followed by the URL for your personal fork of the workshop_mysite repository.
* Make sure you can "cd" into it:
 
cd workshop_mysite
 
Umm, are you rlelay just giving this info out for nothing?
 
=== The development server ===
 
Let's verify this worked. Run the command:
 
<pre>
python manage.py runserver
</pre>
 
You'll see the following output on the command line:
 
<pre>
Validating models...
0 errors found.
 
Django version 1.2, using settings 'mysite.settings'
Development server is running at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
</pre>
 
You've started the Django development server, a lightweight Web server written purely in Python. The Django maintainers include this web server, but on a "deployment" like alwaysdata.com, you typically tie Django into an existing server like Apache.
 
Now that the server's running, visit http://127.0.0.1:8000/ with your Web browser. You'll see a "Welcome to Django" page, in pleasant, light-blue pastel. It worked!
 
Exit the server by pressing CONTROL-C on your keyboard.
 
=== Fixing security settings ===
 
Right now, everyone in the workshop has the same '''SECRET_KEY'''. According to the Django documentation, that is bad. So open up settings.py in your editor (for example, Komodo Edit).
 
'''settings.py''' is a Python script that only contains variable definitions. (Django looks at the values of these variables when it runs your web app.)
 
Find the variable named <tt>SECRET_KEY</tt> and set it to whatever string you want. Go on, we'll wait.
 
=== Database setup ===
 
Keep looking at settings.py: The DATABASES variable is a dictionary with one key: '''default'''.
 
The value is itself another dictionary with information about the site's default database. You can see from the ''NAME'' that the Django project uses a file called ''database.db'' to store information.
 
'''Pop quiz''': Does database.db exist right now?
 
While you're editing settings.py, take note of the INSTALLED_APPS setting towards the bottom of the file. That variable holds the names of all Django applications that are activated in this Django instance. Apps can be used in multiple projects, and you can package and distribute them for use by others in their projects.
 
By default, INSTALLED_APPS contains the following apps, all of which come with Django:
 
* django.contrib.auth -- An authentication system.
* django.contrib.contenttypes -- A framework for content types.
* django.contrib.sessions -- A session framework.
* django.contrib.sites -- A framework for managing multiple sites with one Django installation.
* django.contrib.messages -- A messaging framework.
 
These applications are included by default as a convenience.
 
Each of these applications makes use of at least one database table, so we need to create the tables in the database before we can use them. To do that, run the following command:
 
python manage.py syncdb
 
The syncdb command looks at the INSTALLED_APPS setting and creates any necessary database tables according to the database settings in your settings.py file. You'll see a message for each database table it creates, and you'll get a prompt asking you if you'd like to create a superuser account for the authentication system. Go ahead and do that.
 
== Part 1.5: Creating polls ==
Anonymous user