/djangotutorial

Implementation of the official djangoproject tutorial

Primary LanguagePython

Django related documentation

I want to know how the django framework works. Due to this, i'm following this tutorial, to play around with this stuff.

What have i done?

I already have python, so i don't need to setup it. Only the virtualenv is necessary to be created, so i followed the tutorial, and created it with the following command:

python3 -m venv ~/.virtualenvs/djangodev

I activated the environment with the following command:

source ~/.virtualenvs/djangodev/bin/activate

After the activation, the env name is visible in the terminal:

(djangodev) akosgarai@akos-szemetlada ~/Documents/prog/python $

Now i can use the python command insteead of python3. Also everything is ready to install the django:

python -m pip install Django

Finally let's check that the instalation was successful or not. I can try to get the version of the installed django:

python -m django --version

The output:

3.0.4

The installation was successful.

Create project

Now everything is ready to create a new django project. I have done it with this command:

django-admin startproject djangotutorial

And now the initial project is ready. I can start it with the following commands:

cd djangotutorial
python manage.py runserver

I can check it in the browser: http://127.0.0.1:8000/

At this point, it would worth to make a commit, so that i have done it. Also some ignorable files and patterns were added to a gitignore file and it was committed also.

Create application

I am in the project directory (here is the manage.py), so i can run this command to create the polls application:

python manage.py startapp polls

Let's update (based on this doc) the polls/views.py file, create the index route. The respose is static string. After this step, I have to create the polls/urls.py file and add there the relevant codeblock. The last step is updating the djangotutorial/urls.py file, to register the new path. The app is testable in the browser (http://127.0.0.1:8000/polls/) after the server start command.

python manage.py runserver

In the meantime, i found that i have committed some .pyc files. It's unnecessary, so i removed them with the following command: git rm --cached \*.pyc and also the gitignore file was extended with them.

Database setup

The implementation is based on this and this tutorials.

Switch to postgres user.

sudo su - postgres

Create the database.

createdb djangotutorial

Login to db with psql command.

psql

Create a new user to the database.

CREATE USER djangotutorial WITH ENCRYPTED PASSWORD 'password-is-here';

And finally grant privileges to the user.

GRANT ALL PRIVILEGES ON DATABASE djangotutorial TO djangotutorial;

Now we have the empty db. Let's get some content. In the project directory, we can migrate the database.

python manage.py migrate

This command returned with error. I have missing dependencies. (python psycopg2, and some apt-get stuff)

sudo apt-get install libpq-dev
python -m pip install psycopg2

After it, the migrate command has to worked well.

python manage.py migrate

Now we can define some models in the polls/models.py. Then we have to register the polls application to our djangotutorial/settings.py file. The name of the application (PollsConfig) is coming from the polls/apps.py. Now we can create db migration files fit the following command:

python manage.py makemigrations polls

And the actual migration with the migrate command:

python manage.py migrate

With the django shell (python manage.py shell) i can define questions and choises. To make the models more readable in the shell, we can implement the __str__ functions. The was_published_recently function is an example of a custom function. It returns some new questions, where the pub_date is in one day.

Admin interface

After creating an admin user (based on the documentation), i need to attach the polls app to the admin interface. I can do this with updating the polls/admin.py file.

Views

We can create views to our application. The views needs to be attached to endpoints. We are able to display the data in django templates. To do this, we have to make a templates directory in the application dir. We also have to make a polls directory insite the newly created template dir. I can put the view templates here. We can rase 404 exceptions, and we can display error page if the content is missing (not found in the db). We can do this with shortcuts. We can handle the POST method in the view. According to the gossips, the generic view is a better stuff, to that it's time to refactor the application.

Tests

As i discovered, the djangotutorial user can't create database. To solve this issue, the grants has been added to it. (postgres user, psql console: ALTER USER djangotutorial CREATEDB;) As far as i see, the test writing is obvious.

Static content

It has to be added under the application directory, same level as template dir. (eg: polls/static/) For example the css file supposed to be added to here: polls/static/polls/style.css. We can load the static content from the html templates.