layout | title |
---|---|
default |
MTEC2002 - Django Lab 1 - Hello World |
###Objectives
- Install Django
- Create a hello world web application by using Django's URL dispatcher, views and templates
###Installing Django In terminal:
-
Install django using pip
{% highlight bash %} pip install django {% endhighlight %}
-
Check your installation
{% highlight bash %} which django-admin.py {% endhighlight %}
You should see the following as a result:
/usr/local/bin/django-admin.py
If nothing is returned, the installation did not work. Look through the output of your pip install command to track down the reason why. You can even run
pip install django
again try to reproduce what happened.
###Prepping your working directory In terminal:
-
Clone the mtec2002_assignments repo into a folder in
Desktop
{% highlight bash %} cd Desktop git clone https://[email protected]/your_user_name/mtec2002_assignments.git {% endhighlight %} -
Change your directory so that you're in
mtec2002_assignments
{% highlight bash %} cd your_user_name {% endhighlight %} -
Check that you're in the right place - this should return something like
/Users/student/Desktop/your_user_rname/mtec2002_assignments
{% highlight bash %} pwd {% endhighlight %} -
Make a directory for
class12
and cd into it {% highlight bash %} mkdir class12 cd class12 pwd {% endhighlight %} This time, you should be in the class12 directory:/Users/student/Desktop/your_user_rname/mtec2002_assignments/class12
.If you're not in this directory, check your path against
/Users/student/Desktop/your_user_rname/mtec2002_assignments/class12
. Find where there is a discrepency and go through these steps again to see how to fix it.
###Starting a new Django project and adding an app In terminal:
- Start your project {% highlight bash %} django-admin.py startproject helloworld {% endhighlight %} List the files that were created. {% highlight bash %} ls {% endhighlight %} This should return: {% highlight bash %} helloworld {% endhighlight %}
-
Add an app to your project by using manage.py.
{% highlight bash %}
cd helloworld
python manage.py startapp hello
{% endhighlight %}
List the files that were created.
{% highlight bash %}
ls
{% endhighlight %}
This should return:
hello helloworld manage.py
List the files in the hello app. {% highlight bash %} ls hello {% endhighlight %} This should return:__init__.py models.py tests.py views.py
###Running the dev server In terminal:
-
Make sure you're in your
helloworld
project directory. If you're not,cd
into it. -
Run the dev server
{% highlight bash %}
python manage.py runserver
{% endhighlight %}
This should return the following:
Validating models...
0 errors found Django version 1.4, using settings 'helloworld.settings' Development server is running at http://127.0.0.1:8000/ Quit the server with CONTROL-C.
-
You can view your application at:
http://localhost:8000/
###Mapping a URL to a view
Create a url at /hello
-
With the dev server running, verify that the URL does not exist yet. Open
http://localhost:8000/hello
in your browser. You should see a page that has an error:Page not found (404)
. - Add the new URL to your application. Open
helloworld/helloworld/urls.py
in your text editor (this should be in your class12 folder). - Before the last set of closing parentheses, add this line (including the comma): {% highlight python %} url(r'^hello/$', 'hello.views.hello'), {% endhighlight %}
###Checking your work so far
-
Check your work by going copying and pasting
http://localhost:8000/hello
into your browser -
You should get a page that says:
ViewDoesNotExist at /hello
This means that a view function has to be written. A view was mapped to in urls.py, but it wasn't mapped to yet.
###Creating a view
- Open
helloworld/hello/views.py
in your text editor (this should be in your class12 folder). - Add the following to the file {% highlight python %} from django.http import HttpResponse def index(request): return HttpResponse("Hello world.") {% endhighlight %}
-
Open
http://localhost:8000/hello/
in your browser. You should see:Hello world.
- Try modifying what the index method returns so that it responds with an actual html document. Try wraping the text in strong tags.
###Using a template
- Open
helloworld/hello/views.py
in your text editor (this should be in your class12 folder). - Add this to the top of the file: {% highlight python %} from django.template import Context, loader {% endhighlight %}
-
Replace the body of the index function (that is, the entire
return
statement) with this code: {% highlight python %} t = loader.get_template('hello/index.html') d = {} c = Context(d) return HttpResponse(t.render(c)) {% endhighlight %} -
Open
http://localhost:8000/hello/
in your browser. You should see:TemplateDoesNotExist at /hello/
- Use
pwd
to make sure that you're in/Users/student/Desktop/yourusername/mtec2002_assignments/class12/helloworld
. If you're not: {% highlight bash %} cd /Users/student/Desktop/yourusername/mtec2002_assignments/class12/helloworld {% endhighlight %} -
Make a
templates
directory in yourhelloworld
project's directory {% highlight bash %} mkdir -p templates/hello {% endhighlight %} -
Create a file called
helloworld/templates/hello/index.html
in your text editor. - {% highlight html %} hello world {% endhighlight %}
-
Add the templates directory to your application's settings. Open
helloworld/helloworld/settings.py
. Find this block of code. {% highlight python %} TEMPLATE_DIRS = ( # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates". # Always use forward slashes, even on Windows. # Don't forget to use absolute paths, not relative paths. ) {% endhighlight %} Add this line (your templates folder) before the closing parentheses: {% highlight python %} "/Users/student/Desktop/yourusername/mtec2002_assignments/class12/helloworld/templates/", {% endhighlight %} -
Restart your development server by pressing control-C and
python manage.py runserver
-
Try adding a variable to your view and displaying it in your template by using this in your view:
d = {'greeting': 'hello'}
###Other things to try
-
Add another url, view, and template:
It should it display howdy universe in italics.