Quantcast
Channel: CodeSection,代码区,Python开发技术文章_教程 - CodeSec
Viewing all articles
Browse latest Browse all 9596

Discussing The Basics of Django With You

$
0
0

You:Django?? What is it?

Me:Do you know about python?

You:Yes, I have abasic idea of python. It is a scripting language.

Me:Yes, and?

You:It is a multi-paradigm, object-oriented, structured programming language. It is simple and easy to learn. It has inbuilt modules and packages which makesa program modular and reusable.

Me:Good. Now back to your question, what is Django ?

Django is a python’s framework. It is free and open source web application framework. It follows MVC architectural pattern .

You:Okay okay, but why should I use this framework?

Me:Yes, that’s an interesting point. You can use it when you want-

Rapid development Clean and pragmatic design High Performing, elegant web applications DRY code

It also provides inbuilt admin app which will have all admin related features. We will discuss it more in an example.

You:I have worked on Rails. It also has same features that you described above then how is it different from Rails?

Me:Excellent question. Thank you for asking. Here are some differences-

In Django you need to explicitly define everything URLs, routes, and other configuration files. So you need afull understanding of how anapplicationworks and its flow. Whereas, in Rails everything is automatic. It will set up routes for you. Rails has more magic . Django is more direct. Rails could be more difficult when you are stuck and want to debug whereas Django is explicit. Both have inbuilt libraries that we can use to enhance or add new features. In Ruby it is Rubygems, and in Python it is Package Index. Python is used by Google, Pinterest, Instagram, Disqus whereas Ruby is used by Apple, Twitter, Airbnb, Github. If you want to build very quick web application and want to work on only web applications then Ruby on Rails is a good option but if you want to learn a language that is used more popularly and want to know what is happening behind the scene then you can check out Django with Python.

You:Okay, I am getting the basic idea of Django but not getting confidence on it.

Me:You will never be confident on any framework until you have worked on it. Let’s start by creating a new application.

Here we will be solving polymorphic association problem in Django.

Let’s say, we have 3 models

Post Article Page

Now all 3 models can have images on it. Now we need another model that is ‘ Image ‘.

Now one solution is to save post_id, article_id, page_id in Image table which is correct but not efficient because in each record, two out of three foreign_keys will be blank.

Let’s solve this problem in Django admin.

Let’s start by creating new project called ‘journal’ and I am assuming you will install Django on your own.

django-admin startproject journal

It will create following directory structure

journal/ manage.py journal/ __init__.py settings.py urls.py wsgi.py

Now run your project after going to journal directory.

cd journal

python manage.py runserver

Now we will create new app in this project by running command-

python manage.py startapp blog

It will create following directory structure-

blog/ __init__.py admin.py apps.py migrations/ __init.py models.py tests.py urls.py views.py

You need to add blog in INSTALLED_APPS in settings.py


Discussing The Basics of Django With You

Run thefollowing command to migrate admin related tables

<em>python manage.py migrate</em>

Create superuser

python manage.py createsuperuser

Now you can log in to app with your new credentials at localhost:8000/admin, you would see like this-


Discussing The Basics of Django With You
Now add new models in models.py.
Discussing The Basics of Django With You

Every time when you change in models then you need to create migrations for it. Follow below commands for the same.

<em>python manage.py makemigrations</em>

python manage.py migrate

We will control these models from admin app so we need to register these models in admin.py


Discussing The Basics of Django With You

We can control instances of these models at localhost:8000/admin


Discussing The Basics of Django With You
Now we want to add Image for all of the above models. But instead of adding 3 foreign keys we will add two columns which will work for threeor more foreign keys. Let’s create new model in models.py
Discussing The Basics of Django With You

Note in imageable_type I have used ContentType which is a table of all models. In our case this table will have following rows:

id | app_label | model

―-+―――――+――――――

1 | blog | post

2 | blog | article

3 | blog | page

Now,

python manage.py makemigrations
python manage.py migrate

It will create new table comment with following columns-

title description imageable_id imageable_type created_on last_modified_on

imageable_id will be used to store the id of foreign_table and

imageable_type will be used to store id of class that is stored in content_type table

Currently at/admin/blog/article/add/ page there are only article attributes, but we want to add multiple images while creating article. To do so we need some modifications in admin.py


Discussing The Basics of Django With You

In UI it will be like this-


Discussing The Basics of Django With You

Like this, you can add images in post, page models too.

You:That’s veryhelpful. I can start with this and if I face any issuesthen I will get back to you via comment. Thank you.


Viewing all articles
Browse latest Browse all 9596

Trending Articles