Django: Building a basic site with Admin
Moving my site I realized that I have’t wrote any post about Django. I was sure enough to have been done it. So I must solve this problem XD
What is that?
Django is a framework to build web applications. It is centered on the backend, offering you templates to use in the frontend, but it is possible that you need another additional framework for this layer, just like JQuery , Dojo , Mootools , Prototype , etc.
Installation
We can install it by using easyinstall , althought it has already a Debian package. You can isolate the Django installation, in order to clone it easily.
Creation
Let’s see how to build a little CMS. It is going to contain posts and nothing more. With this goal, the first step will be telling to Django about creating the site:
|
|
Before continuing on, let’s configure our database, modifying settings.py
file, where we must write the database to use. By the way, we will update the installed applications to use “admin” and our new module.
|
|
Leave everything else untouched.
Now we can run the server, but it is going to do very few things:
|
|
And we can open the site, that can be found at https://127.0.0.1:8000.
The model
Let’s edit the model (blog/models.py
), and write a very basic model:
|
|
As you can see, we will have a title and a body. It is not necessary anything else right now.
URLs
Now we are going to update the file urls.py
. This file is going to be used to
allow django to decide what to do with each URL that is requested. That is the
reason it works with regular expressions, allowing it to identify which service
or HTML must process for each case. We need to uncomment django-admin lines
(urls.py
file):
|
|
Administration
We are going to configure the administration tool, to allow us to edit our
entries. We are going to use the file blog/admin.py
|
|
The View
In the URLs file we can write that, when the URL blog/
is found, we will see
blog.views.list
. It is not defined yet, and we will do it in file
‘admin/views.py
:
|
|
With this step we have just made a dependency: blog/list.html
. In this case,
we will need to build a template. Templates are something away from our project,
and they are isolated of it. When you build a Django site, modules are installed
in a place, each of them installed as isolated as it can be possible from
others. If it is possible, they must have no dependency with the others (if it
is possible, I said). It will be templates which give our project the
homogeneity and join all the site. We wrote a path for TEMPLATE_DIRS in
the ‘settings.py’ file. You must create the file blog/list.html
there:
We are using the variable post_list
in our template, looping on it and rendering it how we want.
How to use it
Everything is done!! Hmmm… And what’s next?
|
|
Well, we are going to use it. First of all, let’s assure our database is updated and let’s run the server:
|
|
Now everything is working. We can open the URL that the message was saying to us: https://127.0.0.1:8000 , what carries us to a wonderful “page not found” error. As far as we have debugging option enabled, we can read what happened: The requested URL does not match any defined patterns.
Let’s try the admin interface: https://127.0.0.1:8000/admin . It will ask us the password (that we choose when we did first “syncdb”) and we enter as administrators. We should see three blocks:
- Auth
- Blog
- Sites
If you can see them it is because you did everything right. Create a couple of posts in the “Blog” module. It is not difficult enough.
All these stuff related to the admin interface has been created automatically by using “django-admin” module.
Do you have 2-3 posts? All right. Let’s see the view: https://127.0.0.1:8000/blog . There is nothing to say here but we must be seeing our blog :D
More information
As you can imagine, the wonderful django tutorial :D.
Goodbyes
If I see this issues are interesting, maybe some day I will continue with this little manual. There are a lot of things to be done now:
- To paginate entries if there are a lot
- To create a CRUD interface to avoid to edit posts from admin interface
- To give some color to the site.
- Add tags to our posts.
- Sort them by publishing date.
- Manage users
- Add some tests
- [Think whatever you want and write it here]