Techie January 2021
This tutorial will focus on how to create a standard Django CRUD project.
This tutrial will cover the following: | ||||
---|---|---|---|---|
1. URLs | ||||
2. Templates | ||||
3. Views | ||||
4. Models | ||||
5. Django Admin |
If you haven’t installed and configured virtual environment and the virtualenvwrapper on your machine, I have a tutorial that will show you how to. Click here to read it.
1. Create a Virtual Environment
mkvirtualenv my_website
2. Install the latest version of Django
pip install django
Verify and check the version installed
django-admin --version
3. Create the Django Project
django-admin startproject my_website
# cd into the project:
cd my_website
Run the development server to make sure everything is ok so far
python manage.py runserver
Now if you navigate to http://localhost:8000/ on your browser and you should see something like this:
Now close the development server with Ctrl + C
4. Create a Django App
A django project can have multiple apps in it. For example, you can have an ecommerce app and a blog app within one project. With this approach, Django allows you to manage the apps independently and you can easily move your apps into other projects if need be.
python manage.py startapp blog
Open the settings.py file and specify the allowed hosts as well as add the blog app to installed apps. You should have something similar to this:
At this point, the project tree should resemble this:
6. Create a Templates Directory
If you are familiar with Ruby on Rails, Templates in Django are the equivalent of Views in Rails. Basically, this is where the HTML files go. Open the blog directory and create a directory called templates.
7. Create a Blog Sub-Directory
Within the templates directory, create a sub-directory called blog. For every app in your project, you must create a sub-directory within the templates directory with the same name as your app. For instance, if we created another app in our project called ecommerce, we would then create a sub-directory inside templates directory called ecommerce.
That’s it! You have successfully set up, configured and tested python.
See you in the next tuts