How to make your first webpage using Django3.2 on Windows?
As you’ve already heard of one of the most famous programming languages on the earth, i.e., Python which is famous for its batteries included style of implementation. A beginner-friendly as well as a well-built abstraction that is helpful when you want your work done but still want to have some extra powers of a programmer to be flexible about designs, algorithms, etc. As I suppose, you all will be already familiar with the web framework Django, which has capabilities to make complex websites and have its mother language as known as Python – batteries included feature, (though abstraction has its demerits).?
So to initially get started with Django, you should have Python installed on your computer. If you’ve not installed it yet, install it from?python.org.?After python is installed, you’re halfway there to make your first app if you’re following any regular Django tutorial. But what I want you to do is to create a virtual environment for your project to keep the dependencies of the project separate from what are in your operating system’s global environment. For now, I suggest you to follow these simple commands on your command line.
mkdir newdjangoproject
cd .\newdjangoproject\
Python –m virtualenv env?(will create an env folder with your environment stuff installed, don’t get intimidated by the file structure as you’ll rarely deal with all of them.)
.env\scripts\activate?(activates environment)
deactivate??(deactivates environment)
pip freeze (If you want to list all the modules installed in your virtual environment )
I suggest at least being familiar with the process as it is important to be for a web developer to handle as well as pass all the dependencies in their original form to the person who’s handing the project later. With the environment activated, you should install Django in one go with a simple command?pip install django,?where pip is the python package manager that manages all the modules we install, update, delete etc. This will now install the latest version of Django in your virtual environment. After this tedious task of seeking and understanding, there is a step of self-attainment where you are just two steps away from beginning your journey as a web developer. On the same folder, you’ve installed all the stuff, on your favourite command-line interface, type?python –m django startproject newproject.?Then to navigate to the main project folder having the same name as the outer folder,i.e.,newproject, type?cd newproject?on the terminal. The outer folder can be named as you wish but the folder having ‘manage.py’ file must not be renamed if you are just a beginner. Django divides its web application into a project and many apps inside it, also having the purpose of reusability in different Django projects. Type?python manage.py startapp testapp?and a new folder will be created with a different bunch of folders.?
Navigate to ‘settings.py’ of the inner ‘newproject’ folder and in the python list of INSTALLED APPS, add ‘testapp’ with the already given list of apps. Don’t forget to save each file once you edit anything. Go to ‘views.py’ of the testapp folder and type the following code
from django.http import HttpResponse
from django.views import View
class FirstView(View):
领英推荐
????def get(self,request,*args,**kwargs):
html = “<html><body>My first web page</body></html>”
return HttpResponse(html)
In urls.py of inner ‘newproject’, in the python list of urlpatterns, add?
this line of code after importing views.py module (from testapp import views)?from testapp–
path(‘’,views.FirstView.as_view()),
On the terminal after navigating to the ‘manage.py’ containing folder, run?python manage.py runserver.
You’ve successfully built your first Django application. It should be running on port 8000 of localhost of your favourite browser.
Keep Djangoing...
--Ajay Pathak