2.django 操作笔记
1.django 常用命令
#生成迁移文件
python manage.py makemigrations
#执行迁移
python manage.py migrate
2.render 参数提示: ctl + p
3.django html模板语法
<h1>Student list</h1>
<ul>
{% for stu in <students></students> %}
<li>{{ stu.s_name }}</li>
{% endfor %}
</ul>
4.django项目下创建新的工程
#startapp
(dj111) [root@localhost HelloDjango]# python manage.py startapp Three
#在项目setting.py的INSTALLED_APPS中注册app
#app下创建urls.py并添加路由
from django.conf.urls import url
from Three import views
urlpatterns = [
url(r'^index/', views.index),
]
#将app的urls在工程的urls中注册
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^hello',views.hello),
url(r'^hehe',views.hehe),
url(r'^index',views.index),
url(r'^home', views.home),
url(r'two/',include('Two.urls')),
url(r'three/',include('Three.urls')),
]
#app中的views.py添加内容
def index(response):
pass
#app下创建目录templates,并在templates目录下创建html文件
5.django shell
(dj111) [root@localhost HelloDjango]# python manage.py shell
>>> from Two.models import student
>>> stu = student.objects.all()
>>> for s in stu:
... print(s.s_name)
...
Jack jonse
Jerry10
Jerry40
Jerry93
...