In this tutorial, we are going to build a simple but beautiful CRUD application using the Python Web Development Framework called Django.
If you would like to do this project in a virtual environment, I’m sure there are many tutorials to help you create one. After you have created a virtual environment, proceed with this tutorial. If you do not care about virtual environment stuffs, also proceed.
$ pip install Django==2.0.5
2. Verify the Django version installed by running the command below:
$ django-admin --version
$ mkdir django_projects
2. Move into this directory with
3. Create the project with
$ django-admin startproject CRUD $ cd CRUD
4. Run the development server
$ python manage.py runserver
You should see something like this
5. Navigate to http://localhost:8000/ on your browser and you should see something like this:
Congratulations!!!! You are now ready to start building the CRUD app
Let us create an app called Crud in the CRUD project. Close the development server with Ctrl + C
Create the app with
$ python manage.py startapp Crud
So far, we have a directory structure like this
Open the settings.py file to specify the allowed hosts as well as add the Crud app to installed apps. You should have something like the below:
The Post Model
For our Crud app, we would like to be able to Create, Modify, View and Delete a post. Let us now define a post model in our models.py file. Open the models.py file in your favorite text editor and paste the below codes:
from django.db import models class Post(models.Model): title = models.CharField(max_length=120, help_text='title of message.') message = models.TextField(help_text="what's on your mind ...")
def str(self): return self.title
Our post has a title and a message as fields.
Run the commands below to make migrations:
$ python manage.py makemigrations $ python manage.py migrate
Let us now create a superuser to manage our application
Run the following in the terminal:
$ python manage.py createsuperuser
You will be prompted to enter a username, email address, password and password confirmation. If the superuser is successfully created, we can now run the development server and log on to the Django admin page. Run the development server with
$ python manage.py runserver
Navigate to http://localhost:8000/admin on your browser and you should see something similar as below:
Enter the details to log in.
You should see a page similar to that below:
Our post is not displayed here since we have not registered it in the admin.
Open the admin.py file in a text editor and add the following codes:
from django.contrib import admin from .models import Post #add this to import the Post model admin.site.register(Post) #add this to register the Post model
Refresh the page again and this time you should see the post appear as below
Thanks for reading ❤
If you liked this post, share it with all of your programming buddies!
Originally published on https://medium.com
After analyzing clients and market requirements, TopDevelopers has come up with the list of the best Python service providers. These top-rated Python developers are widely appreciated for their professionalism in handling diverse projects. When...
🔥Intellipaat Django course: https://intellipaat.com/python-django-training/ 👉This Python Django tutorial will help you learn what is django web development &...
Build your eCommerce project by hiring our expert eCommerce Website developers. Our Dedicated Web Designers develop powerful & robust website in a short span of time.