What is the app?
An app in Django is a collection of code that performs a specific task. For example, an app might handle user authentication, store data in a database, or generate HTML pages. Apps are reusable, so you can use them in multiple projects. Now, let's create an app.
First of all, You need to set up the project if you have not done yet then check this article.
After creating a project your file structure will look like the below.
myproject
__init__.py
asgi.py
settings.py
urls.py
wsgi.py
manage.py
I will Name my app as home
Now run the following Commands in your project folder to create an app.
python manage.py startapp home
Django will create a folder named home
in your project folder, this will look like this.
myproject/
__init__.py
asgi.py
settings.py
urls.py
wsgi.py
home/
migrations/
__init__.py
__init__.py
admin.py
apps.py
models.py
tests.py
views.py
manage.py
Now take a look at the views.py
.
Django views are Python functions that take HTTP requests and return HTTP responses, like HTML documents.
A web page that uses Django is full of views with different tasks and missions.
Views are usually put in a file called views.py
located in your app's folder.
There is a views.py
in your home
the folder that looks like this:
# home/views.py
from django.shortcuts import render
# Create your views here.
Now create one function in it. let's say index
which will take one argument as request
.
# home/views.py
from django.shortcuts import render,HttpResponse
# Create your views here.
def index(request):
return HttpResponse("hello world")
now goto home/urls.py
. Initially, it is not there so you have to create it.
# home/urls.py
from django.urls import path
from home import views # import views of home in this file
urlpatterns = [
path('', views.index, name='index'), # it will execute index function when path will match
]
The urls.py
the file you just created is specific to the home
application. We have to do some routing in the root directory myproject
as well. This may seem complicated, but for now, just follow the instructions below.
There is a file called urls.py
on the myproject
folder, open that file and add the include
module in the import
statement, and also add a path()
function in the urlpatterns[]
list, with arguments that will route users that come in via 127.0.0.1:8000/
.
Initially, it will look like this.
# myproject/urls.py
from django.contrib import admin
from django.urls import path
urlpatterns = [
path('admin/', admin.site.urls),
]
Now, change this to the following.
# myproject/urls.py
from django.contrib import admin
from django.urls import path,include
urlpatterns = [
path('admin/', admin.site.urls),
path('home/',include("home.urls")),
]
Now, run the server and go to http://127.0.0.1:8000/home.
So, what is happening here?
When a request is made to the web server, the URL of the request is matched against the urlpatterns
list in the urls.py
file, starting with the first item and working down the list. The first pattern that matches the request's URL is used to determine which view should handle the request.
And once the request is handled by a specific view, the response is sent back to the user via the web server and browser.
congrats 🎉🎉 !! you have created your first app successfully!
I hope this tutorial has helped you to create an app with Django. If you have any questions or need further assistance, please don't hesitate to ask.
Thanks for reading ❤️ and follow me on Twitter.