Python Django Project (Basic Homepage):
Requirements(Before Start Project):
- Install Latest Version Python from https://python.org & Install.
- While you will install the Python, Then Click on “Add To Path” option
- Once Installation Would be complete then Open CMD and Type “python” to check It’s install properly or not.
- Now Open CMD Terminal and install Django : type: “pip install django“
- Once Django would be install check it using “django-admin” in CMD Terminal.
Now to Start Project Open Your Command Terminal and Select Folder where you want to create or start your project e.g: C:\user>cd desktop
Now To Start Project Type in CMD “django-admin startproject appname” e.g:
C:\user\desktop> django-admin startproject basicNow, It will create a new folder inside your desktop with named “basic”. Let’s Begin our development section. Follow all steps with focus and eager.
Now once your project will be create go inside your project through “cd basic” and type following commands:
C:\user\desktop\basic> python manage.py runserverAfter successfully running code, It will give you an url e.g: http://127.0.0.1:8000/. Go To this Website and you will see your landing page.
Now open your project into the PyCharm IDE Code Editor and follow all instructions. Now once you open your PyCharm then you don’t need to use Windows terminal seperately. Because PyCharm has inbuilt Terminal inside the editor. So, open You PyCharm editor and open project that you been created recently. Now run the server using python manage.py runserver in a terminal and use other terminal for other kind of functions.
When you will open your project into your PyCharm Code Editor then there will be different kind of files available in that project like:
- manage.py: It used to manage servers, users and networks kind of stuffs. You don’t have to make any changes inside this file.
- db.sqlite3: It’s a light weight database basically used to store light weight data. In the future, When you would work on big project then you have to use big databases like MySQL etc.
Now, There will be a folder with same name as project name basic, Inside this folder there will a different kind of files e.g:
- asgi.py and wsgi.py: These files comes in use when you want to deploy your website on server, Usually in the end.
- settings.py : It’s one of the useful file that we’re going to explore throughout this guide tutorial. In this file you can define Html, CSS path or new apps that you will add etc. Some Important field in this file:
- ALLOWED_HOSTS = [] , (Here, You will define your domain name)
- INSTALLED_APPS = […….] , (Here, you need to add app when you create inside your project)
- TEMPLATES = [….], (Used to define path of Templates)
- STATIC_URL = [….], (used to add external css, etc)
- Urls.py : It’s main Url configuration file, that used to set and delete Urls Path of your new app.
Now, above files are the main root app inside the main Project. which will be used to define and changes settings, url etc. So, now you have to create new apps inside your project. In which You will define functionality and coding kind of stuffs.
So We want to develop password generator web app. Therefore, We just need only one app here inside our project. Let’s create:
C:\user\desktop\basic> python manage.py startapp firstappNow once you execute above code inside your project then It will create entire folder “firstapp” inside main project. Where you will see files e.g: Models.py, admin.py, apps.py, tests.py, views.py
So, Once you would have created your file then you have make changes into settings.py file. Which you have to add app name inside you INSTALLED_APPS Section, e.g:
INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'generator', ]
So, Now open your url given by terminal. You will still see same default page of django and that’s not what we want. we actually want to show our custom page instead of any default.
How Urls.py Works ?
So, First of all we have to understand that how Url works. So, that we can create out custom homepage on specific url.
So, Whenever users enter any Url inside your website then first it’s comes to the urls.py of main root folder. After then It checks that where this particular url are pointed to.
So, Now inside your Urls.py make following changes:
- remove admin url (We don’t need it anymore for this project)
- first import in urls : from firstapp import views
- inside urlpatterns = [ path(‘ ‘, views.home), ]
Now Open Your Views.py inside generator app and edit:
add : from django.http import HttpResponse
Here, You have to define a function home that we’ve recently defined under the urls.py
def home(request): return HttpResponse('Hello It's First Web APP')
Now When you open your website then you will be see “Hello It’s First Web APP”. But What If we want to show custom Html and CSS webpage then you have to follow below steps
Now First, Create A folder inside your firstapp named templates and then inside that template folder create a new folder called firstapp. Now inside this folder create your html file e.g: firstapp/templates/firstapp/home.html
After then insert following code
def home(request): return render(request, 'firstapp/home.html')
It’s all done.
Well this tutorial is just for the beginners. apart from it, there are bunch of stuffs that you need to learn in Python Django Framework especially It’s Framework.
So, I hope you all will like this article. So, Please don’t forget to share this with your programmers friends.