Quick Code
Published in

Quick Code

How to start web development with Python using the Django framework

Photo by Émile Perron on Unsplash
  • user authentication
  • templates, routes, and views
  • admin interface
  • robust security
  • support for multiple database backends

Setting up the development environment

Photo by RawFilm on Unsplash
pip install virtualenv
virtualenv project
On UNIX(Mac and Linux): source project/bin/activateOn Windows: project\env\Scripts\activate.Bat
pip install Django

Starting with Django

After installing everything we need, we can start to develop with the Django web framework.

django-admin startproject djangowebsite
  • manage.py to manage the entire website.
  • urls.py to manage the website’s URLs
  • settings.py to manage the website’s settings
python manage.py runserver
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>

Modify settings.py

To edit the file we open it and about the 57th line you should see something like this:

'DIRS': [],
'DIRS': [os.path.join(BASE_DIR, 'templates')],

Modify urls.py

Before modifying the urls.py file, let’s take a look at it.

from django.contrib import admin
from django.urls import path
urlpatterns = [
path('admin/', admin.site.urls),
]
from django.contrib import admin
from django.urls import path
from django.shortcuts import render
def index(request):
return render(request, 'index.html')
urlpatterns = [
path('admin/', admin.site.urls),
path('/', index),
]

Running your first custom website with Django

To run the website, as we have already seen, you just need to execute:

python manage.py runserver
  • Update the index.html file, insert some good HTML template with CSS and javascript implementation. If you don’t have HTML, CSS or javascript knowledge, you can download some nice HTML templates from the web
  • Publish your website online for free with the pythonanywhere service, you can find a lot of tutorials that explain how to do it.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store