Creating a skeleton Web application with Django
The process is straightforward:
- Use the django-admin tool to create the project folder, basic file templates, and project management script (manage.py).
- Use manage.py to create one or more applications. ...
- Register the new applications to include them in the project.
- Hook up the url/path mapper for each application.
Django is a Python web framework. It is free and open source and has been around since 2005. It is very mature and comes with excellent documentation and awesome features included by default. Some excellent tools it provides are:
- Excellent lightweight server for development and testing.
- Good templating language.
- Security features like CSRF included right out of the box.
There are a myriad of other useful things included in Django but you shall probably discover them as you go along. We are going to be using Django to build our website in this Blog.
Why is Django a good web framework choice?
The Django project's stability, performance and community have grown tremendously over the past decade since the framework's creation. Detailed tutorials and good practices are readily available on the web and in books. The framework continues to add significant new functionality such as database migrations with each release.
I highly recommend the Django framework as a starting place for new Python web developers because the official documentation and tutorials are some of the best anywhere in software development. Many cities also have Django-specific groups such as Django District, Django Boston and San Francisco Django so new developers can get help when they are stuck.
Installing Django
This is a simple pip install. The latest Django version at the time of writing is Django 2.0.2
pip install django
Creating an App
Now that Django is installed, we can use its start script to create a skeleton project. This is as simple as using its admin script in the following way.
django-admin startproject myfirstwebapp
Running this command creates a skeleton django app with the following structure:
myfirstwebapp
├─myfirstwebapp
│ ├── __init__.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
└── manage.py
When you look into the
myfirstwebapp
folder that was created, you will find a file called manage.py
and another folder called myfirstwebapp. This is your main project folder and contains the project's settings in a file called settings.py
and the routes in your project in the file called urls.py
. Feel free to open up the settings.py
file to familiarize yourself with its contents.
Ready to move on? Excellent.
Changing App Settings
Let's change a few settings. Open up the
settings.py
file in your favorite editor. Find a section called Installed Apps which looks something like this.# myfirstwebapp/settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
Django operates on the concept of apps. An app is a self contained unit of code which can be executed on its own. An app can do many things such as serve a webpage on the browser or handle user authentication or anything else you can think of. Django comes with some default apps preinstalled such as the authentication and session manager apps. Any apps we will create or third-party apps we will need will be added at the bottom of the
Installed Apps
list after the default apps installed.
It is important to note that Django apps follow the Model, View, Template paradigm. In a nutshell, the app gets data from a model, the view does something to the data and then renders a template containing the processed information. As such, Django templates correspond to views in traditional MVC and Django views can be likened to the controllers found in traditional MVC.
That being said, let's create an app.
cd
into the first myfirstwebapp
folder and type;python manage.py startapp myapp
Running this command will create an app called howdy. Your file structure should now look something like this.
myfirstwebapp
├── myfirstwebapp
│ ├── __init__.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
├── myapp
│ ├── __init__.py
│ ├── admin.py
│ ├── apps.py
│ ├── migrations
│ ├── models.py
│ ├── tests.py
│ └── views.py
└── manage.py
To get Django to recognize our brand new app, we need to add the app name to the
Installed Apps
list in our settings.py
file.# helloapp/settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'myapp'
]
Once that is done, let's run our server and see what will be output. We mentioned that Django comes with a built in lightweight web server which, while useful during development, should never be used in production. Run the server as follows:
python manage.py runserver
Your output should resemble the following:
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.
Jan 04, 2018 - 07:42:08
Django version 1.9.6, using settings 'myfirstwebapp.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
If you look carefully, you will see a warning that you have unapplied migrations. Ignore that for now. Go to your browser and access
http://127.0.0.1:8000/
. If all is running smoothly, you should see the Django welcome page.Migrations
Migrations make it easy for you to change your database schema (model) without having to lose any data. Any time you create a new database model, running migrations will update your database tables to use the new schema without you having to lose any data or go through the tedious process of dropping and recreating the database yourself.
Django comes with some migrations already created for its default apps. If your server is still running, stop it by hitting
CTRL + C
. Apply the migrations by typing:python manage.py migrate
If successful, you will see an output resembling this one.
Operations to perform:
Apply all migrations: sessions, auth, contenttypes, admin
Running migrations:
Rendering model states... DONE
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying admin.0001_initial... OK
Applying admin.0002_logentry_remove_auto_add... OK
Applying contenttypes.0002_remove_content_type_name... OK
Applying auth.0002_alter_permission_name_max_length... OK
Applying auth.0003_alter_user_email_max_length... OK
Applying auth.0004_alter_user_username_opts... OK
Applying auth.0005_alter_user_last_login_null... OK
Applying auth.0006_require_contenttypes_0002... OK
Applying auth.0007_alter_validators_add_error_messages... OK
Applying sessions.0001_initial... OK
Running the server now will not show any warnings.
#Urls & Templates
When we ran the server, the default Django page was shown. We need Django to access our
myapp
app when someone goes to the home page URL which is /
. For that, we need to define a URL which will tell Django where to look for the homepage template.
Open up the
urls.py
file inside the inner myfirstwebapp
folder. It should look like this. # myfirstwebapp/urls.py
"""myfirstwebapp URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/1.9/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: url(r'^$', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: url(r'^$', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.conf.urls import url, include
2. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls'))
"""
from django.conf.urls import url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
]
As you can see, there is an existing URL pattern for the Django admin site which comes by default with Django. Let's add our own url to point to our howdy app. Edit the file to look like this.
# myfirstwebapp/urls.py
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^', include('myapp.urls')),
]
Note that we have added an import for
include
from django.conf.urls and added a url pattern for an empty route. When someone accesses the homepage, (in our case http://localhost:8000), Django will look for more url definitions in the myapp
app. Since there are none, running the app will produce a huge stack trace due to an ImportError
..
.
ImportError: No module named 'myapp.urls'
Let's fix that. Go to the
myapp
app folder and create a file called urls.py
. The myapp
app folder should now look like this.├── myapp
│ ├── __init__.py
│ ├── admin.py
│ ├── apps.py
│ ├── migrations
│ │ ├── __init__.py
│ ├── models.py
│ ├── tests.py
│ ├── urls.py
│ └── views.py
Inside the new
urls.py
file, write this.# myapp/urls.py
from django.conf.urls import url
from myapp import views
urlpatterns = [
url(r'^$', views.HomePageView.as_view()),
]
This code imports the views from our
myapp
app and expects a view called HomePageView
to be defined. Since we don't have one, open the views.py
file in the myapp
app and write this code. # myapp/views.py
from django.shortcuts import render
from django.views.generic import TemplateView
# Create your views here.
class HomePageView(TemplateView):
def get(self, request, **kwargs):
return render(request, 'index.html', context=None)
This file defines a view called
HomePageView
. Django views take in a request
and return a response
. In our case, the method get
expects a HTTP GET request to the url defined in our urls.py
file. On a side note, we could rename our method to post
to handle HTTP POST requests.
Once a HTTP GET request has been received, the method renders a template called
index.html
which is just a normal HTML file which could have special Django template tags written alongside normal HTML tags. If you run the server now, you will see the following error page:
This is because we do not have any templates at all! Django looks for templates in a
templates
folder inside your app so go ahead and create one in your myapp
app folder.mkdir templates
Go into the templates folder you just created and create a file called
index.html
(env) django_project/myfirstwebapp/myapp/templates
> touch index.html
Inside the
index.html
file, paste this code.<!-- myapp/templates/index.html -->
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Home Page</title>
</head>
<body>
<h1>Howdy! I am Learning Django!</h1>
</body>
</html>
Now run your server.
python manage.py runserver
You should see your template rendered.
Linking pages
Let's add another page. In your
myapp/templates
folder, add a file called about.html
. Inside it, write this HTML code:<!-- myapp/templates/about.html -->
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>About Us!</title>
</head>
<body>
<h1>Welcome to the about page</h1>
<h3>
Synergetics is an innovative training company that focuses on teaching Software technology by innovating on curriculum <br>
and delivery methodology to meet the diverse need of our clients.<br>
Our instructors are some of the brightest and most knowledgeable in the industry, <br>
and are made up of Microsoft Most Valuable Professionals (MVPs) and <br>
</h3>
<a href="/">Go back home</a>
</body>
</html>
Once done, edit the original
index.html
page to look like this.<!-- myapp/templates/index.html -->
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Home Page!</title>
</head>
<body>
<h1>Howdy! I am Learning Django!</h1>
<a href="/about/">About Me</a>
</body>
</html>
Clicking on the
About me
link won't work quite yet because our app doesn't have a /about/
url defined. Let's edit the urls.py
file in our myapp
app to add it.# myapp/urls.py
from django.conf.urls import url
from howdy import views
urlpatterns = [
url(r'^$', views.HomePageView.as_view()),
url(r'^about/$', views.AboutPageView.as_view()), # Add this /about/ route
]
Once we have added the route, we need to add a view to render the
about.html
template when we access the /about/
url. Let's edit the views.py
file in the myapp
app.# myapp/views.py
from django.shortcuts import render
from django.views.generic import TemplateView
class HomePageView(TemplateView):
def get(self, request, **kwargs):
return render(request, 'index.html', context=None)
# Add this view
class AboutPageView(TemplateView):
template_name = "about.html"
Notice that in the second view, I did not define a
get
method. This is just another way of using the TemplateView
class. If you set the template_name
attribute, a get request to that view will automatically use the defined template. Try changing the HomePageView
to use the format used in AboutPageView
.
Running the server now and accessing the home page should display our original template with the newly added link to the about page.
Clicking on the
About me
link should direct you to the About
page.
On the
About me
page, clicking on the Go back home
link should redirect you back to our original index page. Try editing both these templates to add more information about you.#Conclusion
That was just a brief look at how to get a Django website up and running. You can read more about Django on the official Django docs.
No comments:
Post a Comment