Sunday, April 24, 2016

Django | Create URLs | Create Views | Create Templates

1. Create URLs

We want 'http://127.0.0.1:8000/' to be a homepage of our Application and display custom design / content.

We also want to keep the AppName/urls.py file clean, so we will import urls from our application to the main ProjectName/urls.py file.

Go ahead, add a line that will import AppName.urls into the main url (''). Note that we are using the include function here so you will need to add that to the import on the first line of the file.

Your ProjectName/urls.py file should now look like this:

from django.conf.urls import include, url
from django.contrib import admin

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'', include('AppName.urls')),
]

Django will now redirect everything that comes into 'http://127.0.0.1:8000/' to AppName.urls and look for further instructions there.

If we use     url(r'^abcd/$', include('AppName.urls')), in above code, then http://127.0.0.1:8000/ will be unchanged and http://127.0.0.1:8000/abcd/ will be redirected to AppName.urls

When writing regular expressions in Python it is always done with r in front of the string. This is a helpful hint for Python that the string may contain special characters that are not meant for Python itself, but for the regular expression instead.

Alright ! We redirected the homepage to AppName.urls. Now we need to write something in AppName/urls.py file to show some content in that page.

Create a new AppName/urls.py empty file and add these two first lines:

from django.conf.urls import url
from . import views

Here we're importing Django's function url and all of our views from our application (we don't have any yet, but we will get to that in a minute!)

After that, we can add our first URL pattern:

urlpatterns = [
    url(r'^$', views.activities_list, name='activities_list'),
]

As you can see, we're now assigning a view called post_list to ^$ URL. This regular expression will match ^ (a beginning) followed by $ (an end) - so only an empty string will match. That's correct, because in Django URL resolvers, 'http://127.0.0.1:8000/' is not a part of the URL. This pattern will tell Django that views.activities_list is the right place to go if someone enters your website at the 'http://127.0.0.1:8000/' address.

The last part name='activities_list' is the name of the URL that will be used to identify the view. This can be the same as the name of the view but it can also be something completely different.

If you try to visit http://127.0.0.1:8000/ now, then you'll find some sort of 'web page not available' message. This is because the server (remember typing runserver?) is no longer running. Take a look at your server console window to find out why.


It's telling you that there is no attribute 'activities_list'. That's the name of the view that Django is trying to find and use, but we haven't created it yet.


2. Create Views

A view is a place where we put the "logic" of our application. It will request information from the model you created before and pass it to a template.

Views are placed in the views.py file. We will add our views to the AppName/views.py file.


OK, let's open up this file and see what's in there:

from django.shortcuts import render

# Create your views here.

Not too much stuff here yet.




The simplest view can look like this.

def activities_list(request):
    return render(request, 'AppName/homepage.html', {})

As you can see, we created a function (def) called post_list that takes request and return a function render that will render (put together) our template AppName/homepage.html.

Save the file, go to http://127.0.0.1:8000/ and see what we have got.

Another error! Read what's going on now:


To fix the above error, we need to make sure that homepage.html actually exists. (We've mentioned it in above view)

3. Create Templates

Templates are saved in AppName/templates/AppName directory. So first create a directory called templates inside your AppName directory. Then create another directory called AppName inside your templates directory:

AppName
└───templates
    └───AppName

(You might wonder why we need two directories both called AppName - this is simply a useful naming convention that makes life easier when things start to get more complicated.)

And now create a homepage.html file (just leave it blank for now) inside the AppName/templates/AppName directory.

See how your website looks now: http://127.0.0.1:8000/

If you still have an error TemplateDoesNotExist, try to restart your server. Go into command line, stop the server by pressing Ctrl+C (Control and C buttons together) and start it again by running a 

python manage.py runserver command.



All the content you create in hompage.html will be displayed in our http://127.0.0.1:8000/ page from now.


The actual use of templates is not this. Template means a basic strcucture that should be reusable or extendable. For this functionality, please check my next post.




If you enjoyed this post, make sure you subscribe to my RSS feed! Comments are encouraged

No comments:
Write comments