In this tutorial, we’ll discuss about how to make a simple Blog in Django 2.
在本教程中,我们将讨论如何在Django 2中创建简单的Blog。
Our blog will have TITLE, PICTURE, DESCRIPTION (Body), DATE. There will be two pages, one for showing all the blogs at one place with less information and the second one to show each blog in detail.
我们的博客将包含标题,图片,说明(正文),日期。 将有两个页面,一个页面显示一个信息较少的所有博客,第二个页面显示详细的每个博客。
Prerequisite
先决条件
- Django 2.0 or above should be installed. 应该安装Django 2.0或更高版本。
- Create a project 建立专案
Django博客教程 (Django Blog Tutorial)
建立应用程式 (Creating App)
I have created a project named as blog_project. Now we’ll create an app for Blog, So in future, if we need to use Blog in other projects then we can use this app.
我创建了一个名为blog_project的项目。 现在,我们将为Blog创建一个应用程序。因此,将来,如果需要在其他项目中使用Blog,则可以使用此应用程序。
To create an app, open terminal and navigate to your project folder.
要创建一个应用程序,请打开终端并导航到您的项目文件夹。
Type the command below:
在下面输入命令:
python manage.py startapp blog
python manage.py startapp博客
Now add a path for our newly created app in settings.py. Open settings.py and look for INSTALLED_APPS list.
现在,在settings.py中为我们新创建的应用添加路径。 打开settings.py并查找INSTALLED_APPS列表。
INSTALLED_APPS = [
'blog.apps.BlogConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
Add these lines in the last.
在最后添加这些行。
MEDIA_ROOT= os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/pics/'
I have added a path for Blog as first item of the list INSTALLED_APPS. If you’re thinking, from where this BlogConfig have came from instead of Blog, then navigate to your blog app folder and open apps.py file. Here you can see a class name as BlogConfig. This is what we add in settings.py while adding path for app in settings.
我已将Blog的路径添加为列表INSTALLED_APPS的第一项。 如果您在考虑,此BlogConfig的来源而不是Blog的来源,请导航至Blog应用程序文件夹并打开apps.py文件。 在这里,您可以看到一个名为BlogConfig的类名。 这是我们在settings.py中添加的内容,同时在设置中添加应用的路径。