DEV Community

Yusra Muktar
Yusra Muktar

Posted on

Django Architecture: Models, Views and Templates

Django is a Python framework that makes it easier to create web sites using Python.
Django emphasizes reusability of components, also referred to as DRY (Don't Repeat Yourself), and comes with ready-to-use features like login system, database connection and CRUD operations (Create Read Update Delete).

Assumptions-I will assume that you have your development already configured for Django/Python development.
And that you have already started your project


This is the projects settings/structure as you can see we have manage.py
which is a command-line utility that lets you interact with your Django project. It runs the development server, creates database tables, and more.
You can check out https://dev.to/yusra_muktar_/getting-started-with-django-beginner-summary-with-pycharm-jhh
its step by step about getting started with django

I named my project config and arusha and devcode are my apps
under config you can see wide settings and configurations

config/
init.py
settings.py
urls.py
asgi.py
wsgi.py
Specifications of what each of them does:)

  1. init.py: A file that tells Python that all files in the directory should be considered a Python package. Without this file, we cannot import files from another directory which we will be doing a lot of in Django!

  2. settings.py: Contains all the project’s settings and configurations.

  3. urls.py: The URL declarations for the project are a “table of contents” of your Django-powered site.

  4. asgi.py: allows for an optional Asynchronous Server Gateway Interface(ASGI) to be run

wsgi.py: An entry point for Web Server Gateway Interface(WSGI) compatible web servers to serve your project. It helps Django serve our eventual web pages.

TEMPLATES
A template in Django is just an HTML file with special placeholders for dynamic data.
A template decides how the webpage looks — the design and layout.
The view sends data to the template, and the template displays it nicely.
In the picture below you can see the file structure too,you then open a html file and insert your codes

We will talk about views in details below and how we modify it and add codes
views are what connect your templates to your project.
Views decide which template to use, and what data to show in it.

VIEWS
Django views are Python functions that take http requests and return http response, like HTML documents.
So, views.py is your project’s brain, deciding what to do when someone clicks something or visits a page.
Views are usually put in a file called views.py located on your app's folder.

if you want to execute a view, we must call it via a URL
Create a file named urls.py in the same folder as the views.py file, and type code in it

The urls.py file you just created is specific for the arusha application. We have to do some routing in the root directory config as well.
I will show you everything lemme first stick to my flow so that you dont get confused
There is a file called urls.py on config folder, open that file and add the include module in the import statement, and also add a path() function in the urlpatterns[] list like this.....


I am using windows and pycharm as my IDE
run the server by executing this command in terminal

python manage.py runserver
Enter fullscreen mode Exit fullscreen mode


and in the browser it should show you/display
A browser displays the final webpage generated from the HTML code inside the Django template. Whatever you put in the template (like text, images, or headings) appears in the user’s browser.

DJANGO MODELS!!!
In Django, a model is what defines your database table. It’s basically a python class that maps to a single table in your database
A model will contain all the essential fields and behaviors of the data you’re storing.

A Model defines the structure of your database tables. It’s a Python class that maps to a single table in your database.

You can learn more on Models on Django’s documentation:

https://docs.djangoproject.com/en/5.2/topics/db/models/

Top comments (0)