Category Archives: Django

Creating Django Personal App – Part 6 – Individual blog posts

The last thing we have to do is to create the urlpatterns for individual blog posts, and the post.html page.

Editing the blog/urls.py

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 blog/urls.py file:

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")),
	]

Continue reading

Creating Django Personal App – Part 5 – Django admin

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:

Django admin

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
Password: 
Password (again): 
Superuser created successfully.
(MyWebsite) ddn_ro@Linux:~/PythonProjects/MyWebsite/mywebsite$ 

Continue reading

Creating Django Personal App – Part 4 – Creating the blog app

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 blog.

Creating the blog app

To create a new app within a Django project, run the following command in the terminal:

python manage.py startapp blog

Register the blog app in the settings.py file

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 = [
    'personal',
    'blog',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

Continue reading

Creating Django Personal App – Part 3 – Building the contact form

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.

Continue reading

Creating Django Personal App – Part 2

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 blog page.

I am going to use Bootstrap and some custom CSS styling.

Creating the header.html file

The header.html file is our template. It will be part of all of the other pages of the website.

Continue reading

Creating Django Personal App – Part 1

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 (home, about, portfolio, and contact) and the other app will be the blog.

The first app we want to create is named personal. It will contain the pages (home, about, portfolio, and contact).

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

Continue reading

Installing Django and setting up the server

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.

Installing Django

Before installing Django, you have to cd into the project folder first and activate your development environment.

ddn_ro@Linux:~$ ls
Calibre Library                        examples.desktop  py.html~
data-types-numbers-number-types.html~  Music             PythonProjects
Desktop                                Pictures          Templates
Documents                              Public            Videos
Downloads                              __pycache__
ddn_ro@Linux:~$ cd PythonProjects
ddn_ro@Linux:~/PythonProjects$ ls
MyWebsite
ddn_ro@Linux:~/PythonProjects$ source MyWebsite/bin/activate
(MyWebsite) ddn_ro@Linux:~/PythonProjects$

Continue reading

How to set up a virtual Python development environment with venv

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
  • Install pipsudo 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
  • Install venvsudo apt-get install -y python3-venv
  • Create a folder for the environments – mkdir PythonProjects
  • Change directory to PythonProjectscd PythonProjects
  • Create the virtual environment – python3 -m venv MyWebsite
  • Activate/Deactivate the virtual environment – source MyWebsite/bin/activate or deactivate for deactivating it.

Continue reading

Deploying Django to production (on Heroku)

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 configuration

In 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')

Replace the your_key_from_the_code_you_commented with the key from the code you have just commented out.

Continue reading