Django study notes 01 | 01 writing a Django application

First, the preliminary work

Installation Pycharmand Pythonrecommended direct installation Anaconda, you do not have to install Pythonthe.

AnacondaI can refer to the installation of this blog: Anaconda-- the most hassle free version of Python

Second, open Pycharm, new Django project

Create a new project, select Django, and then give the project a name from, here named mysite, and finally do not forget Application namealso needed, and then click Create. (Note: The new project comes Djangoframework is 专业版built-in, 社区版do not have this feature, I am a registered student mailboxes with free access)

But I created this is a problem, it has given:

I can not find a solution on the point OK, then enter the project, speculation may be version does not match, so the following needs to be modified Djangoversions and manually created Application.

Third, the modified version of Django

Although it Djangois already the 3.0 version, but I would like to use the Python3.7 + Django 2.2 version of the project to build. The default installation of the latest version of Django version, so I need to uninstall Django 3.0 version and re-install the specified version of Django 2.2.

First open Settings:

Then, click Project Interpreter, and then click Django, and then click on the right side -can be deleted.

需要安装指定版本的 Django 的话,就需要点击右侧的 + ,然后在搜索框输入 django ,点击左上角第一行的 django ,再勾选右侧的 Specify version ,选择自己想要的版本即可。

最后点击上图左下角的 Install Package ,稍等片刻就能安装成功。

四、创建投票应用

PycharmTerminal 中输入:

django-admin startapp app_name                #创建app
python manage.py startapp app_name            #创建app(这个会报错,用上面那行命令)

除了第二种命令会报错这个坑之外,还有一个坑要注意。就是输入的文件位置一定要是有 manage.py 这个文件存在的地方,即如下图所示:

详情可以参考我的这一篇博客:Pycharm 搭建 Django 项目踩坑记录

创建 App 成功之后,左上角大概是这样:

五、启动服务

还是在 Terminal 中,输入:

python manage.py runserver 8000

然后 Terminal 会出现这样的情况:

点击上面这个地址,就会看到下面这个页面:

至此,Django 项目算是初步搭建完成。

六、修改网站设置

看上图可以发现是全英文的,我们可以通过相关设置修改为中文版的网站。

首先找到 setting.py ,注意修改 LANGUAGE_CODETIME_ZONEUSE_TZ 这三个就行了。

修改内容如下:

LANGUAGE_CODE = 'zh-hans' # 语言

TIME_ZONE = 'Asia/Shanghai' # 时区

USE_TZ = False # 应用目前不考虑全球化,因此把 USE_TZ 设置为 False ,这样存储到 database 的时间将和当前时间一致。

最后再运行一下代码:

python manage.py runserver 8000

就能看到网站是中文版了。

七、编写第一个视图

打开 polls/views.py,把下面这些 Python 代码输入进去:

from django.http import HttpResponse


def index(request):
    return HttpResponse("Hello, world. You're at the polls index.")

polls 目录里新建一个 urls.py 文件:

输入:urls,然后点击 Python file ,回车,就创建成功了。

现在应用目录看起来应该是这样:

polls/urls.py 中,输入如下代码:

from django.urls import path

from . import views

urlpatterns = [
    path('', views.index, name='index'),
]

下一步是要在根 URLconf 文件中指定我们创建的 polls.urls 模块。

打开 mysite/urls.py 文件:

在红框里面修改代码如下:

from django.contrib import admin
from django.urls import include, path

urlpatterns = [
    path('polls/', include('polls.urls')),
    path('admin/', admin.site.urls),
]

通过以下命令验证是否正常工作:

python manage.py runserver 8000

打开 http://127.0.0.1:8000/ 会看到这样的页面:

打开 http://127.0.0.1:8000/polls/ 会看到:

Hello, world. You're at the polls index.

27.0.0.1:8000/` 会看到这样的页面:

[外链图片转存中…(img-S9MuFK9h-1580283754843)]

打开 http://127.0.0.1:8000/polls/ 会看到:

Hello, world. You're at the polls index.

八、致谢

Python3.7 + Django2.2 + Bootstrap4 +Pycharm2019 入门01

编写你的第一个 Django 应用,第 1 部分

Pycharm+django2.2+python3.6+MySQL实现简单的考试报名系统

Django2.0入门教程:安装Django2.0

Pycharm+Django手把手入门实战教程1-项目创建

PyCharm新建Django项目时报错Error creating Django application :error on python side.Exit code:1.

发布了298 篇原创文章 · 获赞 391 · 访问量 25万+

Guess you like

Origin blog.csdn.net/Wonz5130/article/details/104107389