Open the terminal and type the following:
cd desktop
cd virtenv
$ python -m django --version
if Django is installed you should see the version of the installation (4.1)
$ django-admin startproject mysite
This will create a mysite directory in your current directory.
cd mysite
$ python manage.py runserver
If everything is in order you should see the following in the terminal
Performing system checks...
System check identified no issues (0 silenced).
You have unapplied migrations; your app may not work properly until they are applied.
Run 'python manage.py migrate' to apply them.
December 20, 2022 - 15:50:53
Django version 4.1, using settings 'mysite.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with COMMAND-C.
If you type the url http://127.0.0.1:8000/ or http://localhost:8000/ into your browser you should see this
Open the folder mysite with your IDE
You should see the following folder structure
mysite/
mysite/
init.py
These files are:
The current directory we are in, the outer mysite/ root directory is a container for your project. Its name doesn’t matter to Django; you can rename it to anything you like.
manage.py: A command-line utility that lets you interact with this Django project in various ways. You can read all the details about manage.py in django-admin and manage.py.
The inner mysite/ directory is the actual Python package for your project. Its name is the Python package name you’ll need to use to import anything inside it (e.g. mysite.urls).
mysite/__init__.py: An empty file that tells Python that this directory should be considered a Python package. If you’re a Python beginner, read more about packages in the official Python docs.
mysite/settings.py: Settings/configuration for this Django project. Django settings will tell you all about how settings work.
mysite/urls.py: The URL declarations for this Django project; a “table of contents” of your Django-powered site. You can read more about URLs in URL dispatcher.
mysite/asgi.py: An entry-point for ASGI-compatible web servers to serve your project. See How to deploy with ASGI for more details.
mysite/wsgi.py: An entry-point for WSGI-compatible web servers to serve your project. See How to deploy with WSGI for more details.
Type command-c in the terminal to stop the server
type the following into the terminal
python manage.py startapp helloworld_app
the following files are files we've not seen before
helloworld_app
init.py
migrations
init.py
admin.py - File with admin definitions for the app - such definitions are needed to access model class instances from the Django admin
apps.py - File with configuration parameters for the app.
models.py - File with database definitions (i.e., model classes) for the app.
tests.py - File with test definitions for the app.
views.py - File with view definitions (i.e., controller methods) for the app.
migrations - The directory that contains migrations applied to the app’s database definitions (i.e., model classes).
Open the file settings.py
Add helloworld_app to the list of installed apps
Now open the views.py file inside the helloworld_app folder
Change the file code to this
from django.http import HttpResponse
def index(request):
return HttpResponse('Hello, World!')
First, we imported the HttpResponse class from django.http module then we made a function, named index(), that takes in a request and returns a HttpResponse object i.e. the string 'Hello, World!'. Note that every view function must take at least one parameter by convention called request.
The final step is to map this view in our URL configurations
Open the urls.py file of the main project. Which should look like this.
First we will import the view from helloworld_app into the file
Adding the following line to the code
from helloworld_app import views
Then we added the path for the view by adding this line to the code
path('',views.index, name="homepage")
The ‘ ‘ in the code connotes the root of the project, that is the home page
Type this into the terminal
python manage.py runserver
And visit the url
Well done
We have created our first Django app