How to start a Django project with Pyenv

You may are asking: “Why should i use a virtualenv ?”

That`s a good question, and the answer is easy:

You use a Virtual Environment for isolate all your project dependencies. When your are developing a Django Project you may need some packages for a specific project and some other packages for other project, even a different version of django. With a Virtual Environment you certify that only the dependencies of your project are installed and with the correct version of each one. You can have a project with Django 1.8 in a virtualenv and another project with Django 1.10 with out any problems. When we think about deployement its even better because you know which packages are needed for the specific project, with a simple pip freeze in terminal inside your VirtualEnv you can check all your packages, it really helps! So i hope at this point you already undestand why use a VirtualEnv.

If you don’t have Pyenv installed please check my other post here.

Before start please check if Pyenv is configured in your computer, just type pyenv -v. If you see something like: pyenv 1.0.0 or any other version you are ready to go!

Starting a Django Project

In terminal go to Documents folder and make a new folder ( I always keep mey projects in Doc folder ), now create a new directory and follow this steps:

  1. mkdir {ProjectName}
  2. cd {ProjectName}
  3. python -m venv {VenvDir} . # Dont forget to put . after your {VenvDir} to make your venv in your current folder
  4. source {VenvDir}/bin/active # Activate your VirtualEnv
  5. pip install django # Install last vesrion of Django
  6. django-admin startproject {ProjectName} # Now your are making a new Django project !

Create your first app

To create your first app ( django work with apps as modules, each app make a specific function in your project ). Example, in a blog project you will have a posts app responsible for manage only the posts and you have a comments app just to manage the comments of your blog. Lets try:

  1. cd {ProjectName}
  2. python strartapp {appName}
  3. In your go to your INSTALLED_APPS and add your app in the end of the list ‘{ProjectName}.{appName}’,

Configuring your first view

In your import your

  1. from {projectName}.{appName} import views as {appName}
  2. url(r’^$’. {appName}.home), # this will look in your inside your app for the home function

In your inside your app

from django.http import HttpResponse def home(request):         return HttpResponse('My First Home')

Now you can type in your terminal:

python runserver

Go to your browser and look your amazing first site!


Originally published at on September 23, 2016.

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.