Django Tutorial
About Lesson

In this Django Tutorial we are going to talk about Django Apps & URL Mapping, in the previuos lesson we have installed our Django library and also we have created our first project in Django, now first let’s talk about Django Apps.

 

 

What are Django Apps?

Django apps are used to group application functionality, a project can contain as many apps as you need. for example, if you want to create a project for a news website , you can create an app for health news, another app for technology news, another app for about me information, another app for contact us. There’s no strict rules to the number of apps in a Django project, the purpose of Django apps is to group application functionality to make work easier. Django apps are normally contained in sub directories inside a project.

 

 

OK now let’s create our first Django App in our mysite project,  for example iam going to call my app as polls, but you can name what you want. you can create the app in django by using this command, make sure that you have changed the directory to your project.

 

 

a subdirectory named polls is created containing the app. By default, upon creating an app its subdirectory includes the following:

  • __init__.py .- Python file to allow app packages to be imported from other
    directories. Note __init__.py is not a Django specific file, it’s a generic file used in
    almost all Python applications.
  • migrations.- Directory that contains migrations applied to the app’s database
    definitions (i.e., model classes).
  • admin.py .- File with admin definitions for the app – such definitions are needed to
    access model class instances from the Django admin.
  • apps.py .- File with configuration parameters for the app.
  • models.py .- File with database definitions (i.e., model classes) for the app.
  • tests.py .- File with test definitions for the app.

 

 

 

Django URL Mapping 

OK now let’s do url routing, the first thing you need to create a view, a view is nothing but it is just python regular functions, for example in here i want to create an Index view and after that i want to map the url with Index view.

 

Open your polls app, after that go to view and add this code in the views.py.

 

 

 

Now we have created our view function, it is time to map this view with the url, so create a new python file at name of urls.py in your polls and add this code.

 

 

 

Now we need to tell django about this file, so open your mysite project, and after that go to urls.py that is located in the mysite project and include this file, you can use include function for this.

 

 

 

Now run your project, make sure to change directory to the mysite project.

 

 

 

And go to, open http://127.0.0.1:8000/ in your browser, you will see this result.

Django Tutorial - Django Apps & URL Mapping
Django Tutorial – Django Apps & URL Mapping