Dave Partner

Web development with Django - SimpleBlogger Project part 2 (project structure)

  • 0 Replies

0 Members and 1 Guest are viewing this topic.

In this tutorial we are going to explore the general structure of a  Django project. We will also look at how to use custom templates and static files like Javascript and CSS files  in your Django project. A typical Django project has a tree structure shown below

The Project
   |__ simplebloggerproject
   |   |__   __init__.py
   |   |__   settings.py
   |   |__   urls.py
   |   |__   wsgi.py
   |__ static
   |    |__ app_name (or project name) - 'namespacing'
   |         |__ css
   |         |__ fonts
   |         |__ images
   |         |__ js
   |__ manage
   |__ simple_blogger (app name)
   |  |
   |  |__  migrations
   |  |      |__   __init__.py
   |  |   
   |  |__  __init__
   |  |__  admin.py
   |  |__  models.py
   |  |__  tests.py
   |  |__  views.py
   |  |__  urls.py

Download or clone the sample project here >> https://github.com/EzechukwuJI/simplebloggerproject.git
__init__.py  - The __init__.py file in any directory indicates to the Python interpreter that the
directory should be treated like a Python package. in other words, it initializes a python package.  This makes it easy to import files from this directory.
With the __init__.py file present in a directory, we can do something like this >> from directory_name import file_name
*More about  __init__  here >> http://mikegrouchy.com/blog/2012/05/be-pythonic-__init__py.html

2. settings.py - As the name suggests, the settings file holds the default and user defined settings for your project.

3. urls.py - At the project level, the urls.py contains the root url of each application in the project.
It is used to create a root(base) url to which application level urls defined within a particular namespace are appended.
This is better explained with an illustration.

In the simplebloggerproject urls.py file we define a root url for the simple_blogger application as follows
   urlpatterns = [
         url('^',      include('simple_blogger.urls',  namespace  = "simple_blogger")),
          url('^',      include('app2.urls',            namespace  = "app2")),
          url('^',      include('app3.urls',            namespace  = "app3"))
 and in the app url (using the simple_blogger application), we can define urls as follows;
      urlpatterns = [
              url(r'^posts/recent-posts/$',  views.recentPostView,  name = 'recent-post'),
With the base urls defined, we can call application level urls in the our templates using
the shortcut like this
             {% url 'app_namespace:url_name' %}, In our example,  {% url 'simple_blogger:recent-post' %}
and in  the views, we can use the built-in reverse function to call the url  using
      reverse('app_namespace:url_name'),  In our example, reverse('simple_blogger:recent-post')

The above line of code return the absolute url string as defined on the left hand side of the url in the app-level urls.py file. In our example we have   'http://domain_name.com/posts/recent-posts/

The goal of using this approach is to ensure loose coupling, which is a core component of the Django design philosophy.
more at >> https://docs.djangoproject.com/en/1.10/misc/design-philosophies/

4. wsgi.py - A Web Server Gateway Interface (WSGI) server implements the  web server side of the WSGI interface for running Python web applications. It directs the server to the  project settings.
more explanation at >> https://www.fullstackpython.com/wsgi-servers.html
5. static - The static directory is where you have all the static files used in your project.
The sub directories are use to store applicable files as indicated.
It is important to group app specific static files and directories inside the app name.
Namespacing helps you stay organized and makes your app portable.

The Application
The other directories are usually for the different apps in the project. In our example - simplebloggerproject, we have
just one application.
By default, a new application has four files, admin.py, models.py, tests.py and views.py. We can add as many files as required.

admin.py - This is where we register models defined in models.py so that they can be viewed in the Django admin interface.

models.py - The models.py file is where we define our database models. Models are Python classes which
inherits from Django's Model class. The classname in the table name in the database while the class attributes are the database
fields in the defined table. See example.
 class Students(models.Model):
          name            =   models.CharField(max_length = 200)
          age               =   models.IntegerField(default = 0)
          field_name    =   field_attribute()

After creating the models classes, we have to make migrations. This is done by running the command below in the command line.
         manage.py makemigrations
This action builds a migration file for the models we have created. The database structure is created when we migrate by running
   manage.py migrate
in the command line.
The last two processes are repeated anytime changes are made to the model class(es) to update the database structure.

Migrating the models builds a database schema, usually with the project name as the database name, model classes as tables
and model class attributes as fields in the tables created.

Views.py - The views.py file is where we control what is/isn't visible to a user on our website, perform actions based
on user inputs and action. Views are can be Python classes in Class Based View (CBV) approach of Python functions (methods) in
Function Based View approach.

urls.py - This is user defined - in that, you create the file yourself. The application level urls.py file is
where app urls are defined as explained above.
We can add other files such as custom_functions.py, forms.py, middleware.py where necessary.

Next, we will look at customizing our templates and using static files in our app.

If you have any questions you can drop it in the comment section or on the open Facebook group (link below) for quicker responses.
Join the Facebook group here >> https://www.facebook.com/groups/466737186852664/

Please signup on this site to get alerts as subsequent tutorials are published. Signup here => http://davepartner.com/register/
See you in the next tutorial.