DEV Community

Cover image for Build your first Django app part 2
DoreenNangira
DoreenNangira

Posted on

Build your first Django app part 2

Introduction

This is a continuation of the last tutorial. If you haven't checked the previous tutorial please check it here
In the previous tutorial we had already started a Django project and ran it in our local server. So let's dive into the next steps:

  • Create a Django app Every Django project should have an app. An app is a feature in your project that does a particular thing. For example, if you want to make a website for posting blogs, you will have a blog app. This app will have a name, a database etc. Before we make an app, make sure you have activated your virtual environment. Navigate to your project root directory where the manage.py file is. Then run the following command: python manage.py startapp blog This will create a blog directory with the following structure:
blog/
    __init__.py
    admin.py
    apps.py
    migrations/
        __init__.py
    models.py
    tests.py
    views.py
Enter fullscreen mode Exit fullscreen mode

Let's briefly talk about the above files.
init.py: enables our folders to be recognized by python as a package
admin.py: a file that contains settings for the admin user ie the owner of the project
apps.py: contains settings for your application
migrations/: A folder that will contain history of changes we make to our database
models.py: This hosts the tables for our database
tests.py: a file that you will use to write tests for your application to ensure it works well
views.py: This is where we write the things we want our application to do. eg if we want our application to display "hello world" on home screen we will define a function for that in views.py then call this function.

  • Register your app Navigate to settings.py file then under installed apps, add your app name there. This will let Django recognize this app. The following is the structure:
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'blog',
]
Enter fullscreen mode Exit fullscreen mode
  • Write your first view Let's now add functionality to our app. We want the user to see the statement "Hello World" when he or she visits our home page. so we will write a function in views.py that displays Hello World.
from django.http import HttpResponse

def home(request):
    return HttpResponse("Hello World")
Enter fullscreen mode Exit fullscreen mode

The HttpResponse module has to be imported and it takes in an argument. The above function says that whenever the home view is called, a response of Hello World should be displayed.

  • Create a urls.py for your app In order to call the home view, we need to define a path to that view. This path is called url. When a user visits this url or link, the home view will be called. So create a new file in your blog folder and name it urls.py Add the following code in the file.
from django.urls import path
from . import views

urlpatterns = [
    path("", views.home, name="first"),
]
Enter fullscreen mode Exit fullscreen mode

When writing your url pattern, the first part is the pattern ie the "" above shows that it is blank meaning that this url is the base url. The first path that opens when a user runs the server, the home page.
The second part of a url pattern is the name of the view we want. So here we want the home view.
The third part is the name of the url. This is important especially when we want to use this url in other parts of our project. So we will just reference its name.

  • Include your app urls in your main urls Now navigate to the urls.py file for your main project. This file is in the same folder as settings.py. Write the following code:
from django.contrib import admin
from django.urls import include, path

urlpatterns = [
    path("blog/", include("blog.urls")),
    path("admin/", admin.site.urls),
]
Enter fullscreen mode Exit fullscreen mode

When you open the urls.py, you will find one url path already there. This is the path to the Django admin which comes by default. Now to make your app urls to be recognized by Django, we need to make Django aware. So we need to tell Django that when it checks for urls, it should also include the urls for our blog app. So we will need to use the include function which is first imported from django.urls. Then we write the url path for all the urls in our blog app. This means that when we want to access any url in our app, the first path will be blog then followed by the exact path specified in our app urls. So for our case, to access url for our home view, the user will first navigate to blog path then the second part of the url will be blank since we left it blank in our home url.

  • Run your server To verify that everything is working well, we need to start our server. Write the command python manage.py runserver. Once the server is running, navigate to http://localhost:8000/blog/ You should see the text Hello World To stop the server, press CTRL + C Congratulations! You made it! You just made a functioning Django app.

Conclusion

We have finally reached the end of our tutorial. However, we have not exhausted everything about Django. I hope that this will act as a basic foundation for those of you who want to get into the world of Django. See you soon! Adios!!

Top comments (0)