The last thing we have to do is to create the
urlpatterns for individual blog posts, and the
We have the list view of our blog posts. Now let’s add the details view so we can display an individual blog post on our website. Here is the
from django.conf.urls import url, include
from django.views.generic import ListView, DetailView
from blog.models import Post
urlpatterns = [
url(r'^$', ListView.as_view(queryset = Post.objects.all().order_by("-date")[:25], template_name = "blog/blog.html")),
url(r'^(?P<pk>\d+)$', DetailView.as_view(model = Post, template_name = "blog/post.html")),
At this point we have a functional website but we are going to unleash the true power of Django, the admin interface. Go to http://localhost:8000/admin and you will get something like shown below:
There is a problem, we don’t have a username and a password. No problem, we are going to create one. It is called the
superuser and to create one just run the following command in the terminal:
python manage.py createsuperuser
This is what I got as output:
(MyWebsite) ddn_ro@Linux:~/PythonProjects/MyWebsite/mywebsite$ python manage.py createsuperuser
Username (leave blank to use 'XXXXX):
Email address: XXXXXXX@gmail.com
Superuser created successfully.
It is time to implement the blog functionality to our website. Although I could make the blog within the
personal app, I decided to make a separate app called
To create a new app within a Django project, run the following command in the terminal:
python manage.py startapp blog
blog app in the
As with any new app of the project, you have to register it in the
settings.py file. The
INSTALLED_APPS part of the
settings.py file will look like shown below:
INSTALLED_APPS = [
Our website has a contact page but we need a contact form so the users can contact us easily. While you can build everything from scratch, I opted for a very simple solution, already built! It is called formspree.io. It is free (there is a paid gold account) and it works flawlessly.
If you decide to build your own send email application then go ahead, it is not that difficult and there are dozens of tutorials that you can follow.
So far we have created the
home page of our
personal application. Let’s modify the template so it will contain all the other pages including a link holder to the
I am going to use Bootstrap and some custom CSS styling.
header.html file is our template. It will be part of all of the other pages of the website.
As I said, we are going to build a personal website that will contain pages and a blog. The website will be made of two apps, one app will be the pages (
contact) and the other app will be the blog.
The first app we want to create is named
personal. It will contain the pages (
The second app will be named
blog and it will function as a blog.
Creating the first Django app
Before starting to create a Django app we have to make sure we are within the correct development environment, it is active, and you are within the folder where
manage.py file is.
(MyWebsite) ddn_ro@Linux:~/PythonProjects/MyWebsite/mywebsite$ ls
db.sqlite3 manage.py mywebsite
In order to follow this post we should take a look at http://dan.saigon.ro/how-to-set-up-a-virtual-python-development-environment-with-venv/.
We have our Python virtual development environment now. Let’s do something with it! I called it
MyWebsite with a reason. I want to build a Python powered website using Django as a framework.
Before installing Django, you have to
cd into the project folder first and activate your development environment.
Calibre Library examples.desktop py.html~
data-types-numbers-number-types.html~ Music PythonProjects
Desktop Pictures Templates
Documents Public Videos
ddn_ro@Linux:~$ cd PythonProjects
ddn_ro@Linux:~/PythonProjects$ source MyWebsite/bin/activate
Below are the steps for creating a virtual Python development environment. If you don’t know what they means then below there is each step explained.
- Update the system –
sudo apt-get update
- Upgrade the system –
sudo apt-get -y upgrade
- Check Python version –
python for Python 2.X, or
python3 for Python 3.X
sudo apt-get install -y python3-pip for Python 3.X
- Install a few extra packages –
sudo apt-get install build-essential libssl-dev libffi-dev python-dev
sudo apt-get install -y python3-venv
- Create a folder for the environments –
- Change directory to
- Create the virtual environment –
python3 -m venv MyWebsite
- Activate/Deactivate the virtual environment –
source MyWebsite/bin/activate or
deactivate for deactivating it.
This is how you deploy your Django application on Heroku. Just five easy steps and your application is online ready for production. Django uses SQLite database by default so we use it too here!
Step 1 – Initial
settings.py comment out the original
SECRET_KEY configuration and add below it the following code:
SECRET_KEY = os.environ.get('DJANGO_SECRET_KEY', 'your_key_from_the_code_you_commented')
your_key_from_the_code_you_commented with the key from the code you have just commented out.