Today we are going to create our first django project. We’ll assume you have Python Installed already. If not, you can see our this post Python Installation .
Create a virtual environment
The venv module provides support for creating lightweight “virtual environments” with their own site directories, optionally isolated from system site directories. Each virtual environment has its own Python binary (which matches the version of the binary that was used to create this environment) and can have its own independent set of installed Python packages in its site directories. to know more details about venv visit this link
Creation of virtual environments is done by executing the command venv:
python3 -m venv /path/to/new/virtual/environment
For example
python3 -m venv env
Activate the virtual environment
If you are using windows execute this command to activate virtual environment
env\Scripts\activate
If you are using linux/ unix/ macOS execute this command to activate virtual environment
source env/bin/activate
Install Django
After you’ve created and activated a virtual environment, enter the command
pip install Django
Creating a project
If this is your first time using Django, you’ll have to take care of some initial setup. Namely, you’ll need to auto-generate some code that establishes a Django project – a collection of settings for an instance of Django, including database configuration, Django-specific options and application-specific settings.
django-admin startproject mysite
Let’s look at what startproject created:
mysite/ manage.py mysite/ __init__.py settings.py urls.py asgi.py wsgi.py
From the command line go to that mysite using cd command
cd mysite
The development server
Let’s verify your Django project works
python manage.py runserver
Now that the server’s running, visit http://127.0.0.1:8000/ with your web browser. You’ll see a “Congratulations!” page, with a rocket taking off. It worked!
Creating the Polls app
To create your app, make sure you’re in the same directory as manage.py and type this command:
python manage.py startapp polls
That’ll create a directory polls, which is laid out like this:
polls/ __init__.py admin.py apps.py migrations/ __init__.py models.py tests.py views.py
The poll application will be stored under this directory structure.
Register into setting.py file
Now you need to register you app name into project setting.py file under INSTALLED_APPS section.
INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'polls', ]
Write your first view
Let’s write the first view. Open the file polls/views.py and put the following Python code in it:
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, world. You're at the polls index.")
In Django, this is the most basic view. To call the view, we must first map it to a URL, which necessitates the use of a URLconf.
Build a file called urls.py in the polls directory to create a URLconf.
In the polls/urls.py file include the following code:
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
The next step is to point the root URLconf at the polls.urls module. In mysite/urls.py, add an import for django.urls.include and insert an include() in the urlpatterns list, so you have:
from django.urls import include, path
urlpatterns = [
path('polls/', include('polls.urls')),
path('admin/', admin.site.urls),
]
You have now wired an index view into the URLconf. Verify it’s working with the following command:
python manage.py runserver
That is it for today, hope it helps. If you have any suggestion for this article please make a comment in comment section below.
—
If you like this article, you can buy me a coffee. Thanks!
Ohh I try this document to create my first Django project. That’s very good.
This website was… how do you say it? Relevant!! Finally I have found something which helped me. Kudos!
Thank you for another informative blog. Where else may I
am getting that type of information written in such an ideal way?
I’ve a venture that I’m simply now working on, and I’ve been on the look out for such information.