Django入门之自定义页面
Posted 侠之大者
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Django入门之自定义页面相关的知识,希望对你有一定的参考价值。
1.创建项目,创建app
django-admin.py startproject HelloWord
python3 manage.py startapp sync_one
#第二步需要进入HelloWord目录执行
2.修改views.py文件
from django.shortcuts import render from django.shortcuts import HttpResponse # Create your views here. def home(request): return HttpResponse("Hello word")
#黄色部分为新增
3.修改url.py文件
"""django1 URL Configuration The `urlpatterns` list routes URLs to views. For more information please see: https://docs.djangoproject.com/en/1.9/topics/http/urls/ Examples: Function views 1. Add an import: from my_app import views 2. Add a URL to urlpatterns: url(r\'^$\', views.home, name=\'home\') Class-based views 1. Add an import: from other_app.views import Home 2. Add a URL to urlpatterns: url(r\'^$\', Home.as_view(), name=\'home\') Including another URLconf 1. Import the include() function: from django.conf.urls import url, include 2. Add a URL to urlpatterns: url(r\'^blog/\', include(\'blog.urls\')) """ from django.conf.urls import url from django.contrib import admin from app01 import views urlpatterns = [ url(r\'^admin/\', admin.site.urls), url(r\'^home/\', views.home), ] #一个url一个函数
4.启动项目
kamil@kamil-ThinkPad-X260:~/PycharmProjects/django1$ python3 manage.py runserver 127.0.0.1:8000 Performing system checks... System check identified no issues (0 silenced). You have unapplied migrations; your app may not work properly until they are applied. Run \'python manage.py migrate\' to apply them.
5.测试
6.后台配置
kamil@kamil-ThinkPad-X260:~/PycharmProjects/django1$ python3 manage.py makemigrations #生成配置文件 kamil@kamil-ThinkPad-X260:~/PycharmProjects/django1$ python3 manage.py migrate
#根据配置文件创建数据库相关 Operations to perform: Apply all migrations: auth, sessions, admin, contenttypes Running migrations: Rendering model states... DONE Applying contenttypes.0001_initial... OK Applying auth.0001_initial... OK Applying admin.0001_initial... OK Applying admin.0002_logentry_remove_auto_add... OK Applying contenttypes.0002_remove_content_type_name... OK Applying auth.0002_alter_permission_name_max_length... OK Applying auth.0003_alter_user_email_max_length... OK Applying auth.0004_alter_user_username_opts... OK Applying auth.0005_alter_user_last_login_null... OK Applying auth.0006_require_contenttypes_0002... OK Applying auth.0007_alter_validators_add_error_messages... OK Applying sessions.0001_initial... OK No changes detected kamil@kamil-ThinkPad-X260:~/PycharmProjects/django1$ python3 manage.py createsuperuser
#创建用户
以上是关于Django入门之自定义页面的主要内容,如果未能解决你的问题,请参考以下文章