Skip to content

Latest commit

 

History

History
278 lines (249 loc) · 7.08 KB

django-lab-1.markdown

File metadata and controls

278 lines (249 loc) · 7.08 KB
layout title
default
MTEC2002 - Django Lab 1 - Hello World

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:

  1. Install django using pip

    {% highlight bash %} pip install django {% endhighlight %}

  2. 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:

  1. 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 %}
  2. Change your directory so that you're in mtec2002_assignments {% highlight bash %} cd your_user_name {% endhighlight %}
  3. 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 %}
  4. 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:

  1. 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 %}
  2. 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:

  1. Make sure you're in your helloworld project directory. If you're not, cd into it.
  2. 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.

  3. You can view your application at: http://localhost:8000/

###Mapping a URL to a view

Create a url at /hello

  1. 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).
  2. Add the new URL to your application. Open helloworld/helloworld/urls.py in your text editor (this should be in your class12 folder).
  3. 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

  1. Check your work by going copying and pasting http://localhost:8000/hello into your browser
  2. You should get a page that says: ViewDoesNotExist at /hello
  3. 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

  1. Open helloworld/hello/views.py in your text editor (this should be in your class12 folder).
  2. Add the following to the file {% highlight python %} from django.http import HttpResponse def index(request): return HttpResponse("Hello world.") {% endhighlight %}
  3. Open http://localhost:8000/hello/ in your browser. You should see:
    Hello world.
    
  4. 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

  1. Open helloworld/hello/views.py in your text editor (this should be in your class12 folder).
  2. Add this to the top of the file: {% highlight python %} from django.template import Context, loader {% endhighlight %}
  3. 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 %}
  4. Open http://localhost:8000/hello/ in your browser. You should see:
    TemplateDoesNotExist at /hello/
    
  5. 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 %}
  6. Make a templates directory in your helloworld project's directory {% highlight bash %} mkdir -p templates/hello {% endhighlight %}
  7. Create a file called helloworld/templates/hello/index.htmlin your text editor.
  8. {% highlight html %} hello world {% endhighlight %}
  9. 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 %}
  10. Restart your development server by pressing control-C and python manage.py runserver
  11. 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

  1. Add another url, view, and template:

    http://localhost:8000/howdy/.

    It should it display howdy universe in italics.