Make a website with Django: Difference between revisions

From OpenHatch wiki
Content added Content deleted
imported>Jesstess
No edit summary
 
(14 intermediate revisions by 6 users not shown)
Line 14: Line 14:
==Project setup==
==Project setup==


=== Install Django ===
'''''Bold text''''Italic text'''''=== Install Django ===


Please follow the instructions in [https://docs.djangoproject.com/en/1.4/intro/install/ the official installation guide] to install Django.
Please follow the instructions in [https://docs.djangoproject.com/en/1.5/intro/install/ the official installation guide] to install Django.


=== Test your installation ===
=== Test your installation ===
Line 33: Line 33:
=== 1. Complete Part 1 of the tutorial ===
=== 1. Complete Part 1 of the tutorial ===


* Please visit https://docs.djangoproject.com/en/1.4/intro/tutorial01/ and complete part 1 of the tutorial.
* Please visit https://docs.djangoproject.com/en/1.5/intro/tutorial01/ and complete part 1 of the tutorial.


In this part, you'll create a new project, learn how to run your development server, set up a database, and create some models for your polling app. Some of the setup that you are doing in this section will make more sense once you start actually using the code a bit later in the tutorial -- don't worry if the purpose of some of the steps you are going through is a bit unclear at first.
In this part, you'll create a new project, learn how to run your development server, set up a database, and create some models for your polling app. Some of the setup that you are doing in this section will make more sense once you start actually using the code a bit later in the tutorial -- don't worry if the purpose of some of the steps you are going through is a bit unclear at first.
Line 43: Line 43:


This tutorial covered a lot of ground quickly. If you have any questions or want to learn more, wave over a staff member!
This tutorial covered a lot of ground quickly. If you have any questions or want to learn more, wave over a staff member!



=== 2. Complete Part 2 of the tutorial ===
=== 2. Complete Part 2 of the tutorial ===


* Please visit https://docs.djangoproject.com/en/1.4/intro/tutorial02/ and complete part 2 of the tutorial.
* Please visit https://docs.djangoproject.com/en/1.5/intro/tutorial02/ and do almost all of part 2 of the tutorial. Stop when you get to the section "Customize the admin look and feel" (you can go back and read it later if you want, but it's not important for your first app).


<br />
<br />
Line 53: Line 52:
<br />
<br />
<b>Check your understanding</b>: What did you have to do to get your poll app displayed on the admin index page?
<b>Check your understanding</b>: What did you have to do to get your poll app displayed on the admin index page?



=== 3. Complete Part 3 of the tutorial ===
=== 3. Complete Part 3 of the tutorial ===


* Please visit https://docs.djangoproject.com/en/1.4/intro/tutorial03/ and complete part 3 of the tutorial.
* Please visit https://docs.djangoproject.com/en/1.5/intro/tutorial03/ and complete part 3 of the tutorial.


<br />
<br />
Line 78: Line 76:
</li>
</li>
<li>
<li>
What is the purpose of <code>t.render</code> in this code snippet?
What is the purpose of <code>template.render</code> in this code snippet?


<pre>def index(request):
<pre>def index(request):
latest_poll_list = Poll.objects.all().order_by('-pub_date')[:5]
latest_poll_list = Poll.objects.order_by('-pub_date')[:5]
t = loader.get_template('polls/index.html')
template = loader.get_template('polls/index.html')
c = Context({
context = Context({
'latest_poll_list': latest_poll_list,
'latest_poll_list': latest_poll_list,
})
})
return HttpResponse(t.render(c))</pre>
return HttpResponse(template.render(context))</pre>
</li>
</li>
</ol>
</ol>



=== 4. Complete Part 4 of the tutorial ===
=== 4. Complete Part 4 of the tutorial ===


* Please visit https://docs.djangoproject.com/en/1.4/intro/tutorial04/ and complete part 4 of the tutorial.
* Please visit https://docs.djangoproject.com/en/1.5/intro/tutorial04/ and complete part 4 of the tutorial.


<br />
<br />
Line 106: Line 103:


[[File:Fireworks.png|150px]]
[[File:Fireworks.png|150px]]
[[File:Balloons.png|150px]]
[[File:Balloons.png|150p

Latest revision as of 13:44, 21 May 2017

Project

Work through the official Django tutorial, where you'll create an interactive poll application.

Goals

  • Learn the basics of the Django web application framework
  • Practice writing classes and thinking about inheritance
  • Practice using Python to interact with a database
  • Think about how web servers and clients interact

Project setup

Bold text'Italic text=== Install Django ===

Please follow the instructions in the official installation guide to install Django.

Test your installation

From a Python prompt, type:

import django

and hit enter. If nothing gets printed, you had no error importing the django library, so you've successfully installed Django!

Project steps

The official Django tutorial walks you through creating an interactive polling application in 4 parts.

1. Complete Part 1 of the tutorial

In this part, you'll create a new project, learn how to run your development server, set up a database, and create some models for your polling app. Some of the setup that you are doing in this section will make more sense once you start actually using the code a bit later in the tutorial -- don't worry if the purpose of some of the steps you are going through is a bit unclear at first.


After you've completed part 1:

Check your understanding: What is the relationship between the classes in models.py and the database schema displayed when you run python manage.py sql polls.

This tutorial covered a lot of ground quickly. If you have any questions or want to learn more, wave over a staff member!

2. Complete Part 2 of the tutorial


After you've completed part 2:

Check your understanding: What did you have to do to get your poll app displayed on the admin index page?

3. Complete Part 3 of the tutorial


After you've completed part 3:

Check your understanding:

  1. Which of the following URL fragments matches this pattern from a sample urls.py?
    r'^polls/(?P<poll_id>\d+)/results/$'
    • polls/ABC/results/
    • polls/12345/results/
    • polls//results/
  2. Why are templates a good idea?
  3. What is the purpose of template.render in this code snippet?
    def index(request):
        latest_poll_list = Poll.objects.order_by('-pub_date')[:5]
        template = loader.get_template('polls/index.html')
        context = Context({
            'latest_poll_list': latest_poll_list,
        })
        return HttpResponse(template.render(context))

4. Complete Part 4 of the tutorial


After you've completed part 4:

Check your understanding: What is in request.POST?

Congratulations!

You've built a website with Python and Django; this is a huge accomplishment. Keep practicing!

[[File:Balloons.png|150p