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
cd django_projects
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.
Make Migrations
Run the commands below to make migrations:
$ python manage.py makemigrations
$ python manage.py migrate
Superuser
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!
Follow us on Facebook | Twitter
☞ Complete Python Bootcamp: Go from zero to hero in Python 3
☞ Machine Learning A-Z™: Hands-On Python & R In Data Science
☞ Python and Django Full Stack Web Developer Bootcamp
☞ The Python Bible™ | Everything You Need to Program in Python
☞ Python and Django Full Stack Web Developer Bootcamp
☞ Build a Backend REST API with Python & Django - Advanced
☞ Python Django Dev To Deployment
☞ Build Your First Python and Django Application
☞ How To Set Up Django with Postgres, Nginx, and Gunicorn on Ubuntu 16.04
☞ Building a Universal Application with Nuxt.js and Django
☞ Django Tutorial: Building and Securing Web Applications
☞ Building a Weather App in Django
Originally published on https://medium.com
#django #python #web-development