When I first got interested in web development, like any other enthusiast, I learned HTML, CSS, and some javascript and created a normal good looking webpage and then I am like wooo man you are a web developer now. But then the concept of backend pops up and I am like wooo this shit is scary, I am way far to become a web developer...
Backend involves many things, like URL routing, and managing forms get-post requests, securities, dynamic webpages, how servers work, how to set up one, and what the hell is a web server.
You know, life was simpler when you know HTML and CSS, but man, all those above pieces of stuff, kind of scary right? at least for me, but yeah if you like those things you might fall in love with them and kind of find your way out. But things should be simpler right? And that's were our Lil friend Django comes in.
Django says, Okay buddy I got you, I will handle those things, but only one thing is that you should know about python. I work with Python, Python is easy and that is why I am.
Django is a super amazing web framework you can go to their official website and read all the documentation and tutorials here.
Now, Let's actually talk about what we meant by Backend and how Django helps us.
We have read what is HTTP response cycle is, you enter your URL to the browser, and then the browser takes your request to the corresponding web server, then web server understands your request and then generates a response back to it and that response is just another webpage.
Now, where is the Django in all these steps? Django covers all the parts were the web server understands your request, and generates a response to it in a form of a webpage. That's what the backend is. understanding the user request, fetching required data from the database, and generating a response according to it.
Now, generating response is the main thing, right? you understand the user's request, now according to that request, you have to generate the response.
URL Routing In Django
Take this example, you write the URL www.example.com/books/moral, Now, the browser will contact the server of www.example.com and sees the URL pattern, Now it says that okay go to the 'books',
Then it will look where the 'books' is located, if it founds then okay, further is 'moral', okay search for 'moral', we found the moral great, proceed to the next step.
The above step is URL Routing, we first understand, where to look and where to go.
After URL routing, The next step is generating a 'View', That is, you have to generate a webpage according to it, How you do it? By understanding the user's request, right? According to it, you have to generate a view.
Views In Django
'View' is what we see, the look, the feel of the webpage. Now, okay we understand the URL Routing, Then View, Now what to show the user? The user is expecting some data right? maybe, the above URL, just lists out the books whose genre is moral, i.e we have to create a view which can list out the books with genre moral.
If we remember the concept of the HTTP request and response cycle, Actually what's happening is, we as a user, when visit any website, request some data. And the data is shown to us as an HTML page,
Now, the data is retrieved from the database, according to the user's request, right? If I want the list of books with genre 'self help', I have to generate the data, and the 'web page' according to it.
So, if your website has let's say 5 geners, 'moral', 'self help', 'thriller', 'science fiction', 'fantasy', will you create 5 web pages? NO! offcourse not, And that's where the notion of dynamic web pages comes into play, and for this the django views will help us.
Let's understand this with an example:
Suppose you have a database with these two tables, a simple one just for example:
bid | bgenre |
---|---|
1 | moral |
2 | self help |
3 | thriller |
4 | science fiction |
5 | fantasy |
book_id | bname | bid |
---|---|---|
1 | The Moral Landscape | 1 |
2 | The 48 Laws Of power | 1 |
3 | The virtue Of Selfisness | 1 |
4 | The Secret | 2 |
5 | Think And Grow Rich | 2 |
6 | The Guest | 3 |
7 | Before I go to sleep | 1 |
Now, User can request any data, right. So, You have to create a view which handles the request dynamically. How we do that? have you ever observed a URL like this www.example.com/books?bid=1
The part with books?bid=1, is a way to tell the server, okay go to the database and fetch the view with data were bid is 1.
So, In short we generate the views like that, dynamically!
We have to contact to our database, okay give me some data of book author, yeah some SQL queries will be performing okay, I don't wanna go there just fetch me the book names with the author who writes the moral books.
And then we took these data, we append it to our view then just shove it back to the User. Simple, That's how a website is working. A whole lot of fuss right, so much thing to do.
And to learn those things, puff my mind just blows up.
But, then Django comes, says, man, I am here I will do those things, Don't worry you will be a web developer, I am gonna help you.
And there we go, Off to learn Django. Cause we want to be a web developer right. cool stuff.
So, let's write a few lines of code, and launch your first website right now. yeah, it's that easy...
okay, we have to create a virtual environment first, but, I don't know how to set up one, what is a virtual environment? Just read it here. Install anaconda, It will be easy.
1. Creating a Virtual Environment
conda create -n learnDjango
2. Activate a Virtual Environment
conda activate learnDjango
3. Install Django
conda install django
4. Start A Project
django-admin startproject first_project
- first_project (folder)
- manage.py
- __init__.py
- asgi.py
- wsgi.py
- urls.py
- settings.py
urls.py
settings.py
launching Website
python manage.py runserver
python manage.py runserver 8080
Watching for file changes with StatReloader Performing system checks... System check identified no issues (0 silenced). You have 18 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions. Run 'python manage.py migrate' to apply them. September 24, 2020 - 16:21:29 Django version 3.1, using settings 'first_project.settings' Starting development server at http://127.0.0.1:8080/ Quit the server with CONTROL-C.
- Introduction (like this page)
- Managing DataBases In Django
- Learning How To Show Views and Templates
Django Templates
Django's DataBase Models
Creating table in Django Using Model
from django.db import models class BookGener(models.Model): bid = models.IntegerField(primary_key=True) bname = models.CharField(max_length=100)
This is the way to create a table 'BookGenre' in django. You can read the official documentation of django models here
Insert Data In Django Model Table
bg1 = BookGenre(1,'Moral')
bg1.save()
Retreiving Data From Django Model
b1 = BookGenre(2,'self help') b1.save() b1 = BookGenre(3,'thriller') b1.save() b1 = BookGenre(4,'science fiction') b1.save() b1 = BookGenre(5,'fantasy') b1.save()
BookGenre().objects.all()
<QuerySet [<BookGenre: BookGenre object (1)>, <BookGenre: BookGenre object (2)>, <BookGenre: BookGenre object (3)>, <BookGenre: BookGenre object (4)>, <BookGenre: BookGenre object (5)>]>
BookGenre().objects.all().values()
<QuerySet [{'bid': 1, 'bgenre': 'moral'}, {'bid': 2, 'bgenre': 'self help'}, {'bid': 3, 'bgenre': 'thriller'}, {'bid': 4, 'bgenre': 'science fiction'}, {'bid': 5, 'bgenre': 'fantasy'}]>
0 Comments