Django is just a framework, which means it only provides basic services to web applications. And the applications are what users create to realize the function they need.
So, we will start with the first Django application.
image by RealPython.
To guarantee that the webserver can keep running stably, we usually use a new local or independent environment while developing the program. That choice is up to you. And only after enough integration and user tests, we make the deployments of source code or replacements of files and packages to the production environment.
Here is a way to create and activate a Django virtual environment via virtualenv.
sudo python3 -m pip install virtualenv
mkdir ~/environments
cd ~/environments
virtualenv -p python3 ./django_env
source ~/environments/django_env/bin/activate
Start a Django application named "myblog".
python3 manage.py startapp myblog
Add myblog into the INSTALLED_APPS list in ~/mysite/mysite/settings.py.
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'myblog', # new added
]
~/mysite/mysite/urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('myblog.urls', namespace='myblog')),
]
~/mysite/myblog/urls.py
from django.urls import path
from . import views
app_name = '[myblog]'
urlpatterns = [
path('', views.index, name='index'),
]
~/mysite/myblog/views.py
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello Guys! This is Russell!")
We must restart the uWSGI server each time, to make the effect of modifications on our applications or Django settings.
sudo killall -9 uwsgi
sudo uwsgi --ini mysite_uwsgi.ini
Now on the test page, the Django rocket may replaced by your greeting words.
www.caorussell.com
Authorize a Django administrator.
python3 manage.py createsuperuser
Login into the admin page.
www.caorussell.com/admin
~/mysite/myblog/model.py
from django.conf import settings
class PostLabels(models.Model):
label_name = models.CharField('Label Name', max_length=20)
label_slug = models.SlugField('Label Slug', unique=True, default='Unique Slug')
label_desc = models.TextField('Label Desc', max_length=300, default='Label Description')
class Meta:
verbose_name = 'Label Name'
verbose_name_plural = verbose_name
ordering=['label_name']
def __str__(self):
return self.label_name
class BlogPosts(models.Model):
post_author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, verbose_name='Post Author')
post_title = models.CharField(verbose_name='Post Title', max_length=150)
post_summary = models.TextField(verbose_name='Post Summary', max_length=300, default='Post Summary')
post_content = models.TextField(verbose_name='Post Content', default='Post Content')
publish_time = models.DateTimeField(verbose_name='Publish Time', auto_now_add=True)
post_slug = models.SlugField(verbose_name='Post Slug', unique=True, max_length=20, default='Unique Slug')
post_label = models.ForeignKey(PostLabels, on_delete=models.CASCADE, verbose_name='Post Label')
class Meta:
verbose_name = 'Post Name'
verbose_name_plural = verbose_name
ordering=['-publish_time']
def __str__(self):
return self.post_title[:20]
~/mysite/myblog/admin.py
from myblog.models import PostLabels, BlogPosts
@admin.register(BlogPosts)
class BlogPostsAdmin(admin.ModelAdmin):
list_display = ['post_title', 'post_label','post_summary', 'post_author','post_content', 'publish_time','id']
list_filter = ('post_author',)
@admin.register(PostLabels)
class PostLabelsAdmin(admin.ModelAdmin):
list_display = ['label_name', 'label_desc', 'id']
make migrate to the database.
python3 manage.py makemigrations
python3 manage.py migrate