跟大妈学Django Part - 8 (Django入门续集8)
今天来做EmoBox页面吧,这样在用户提交表格以后就有地方显示出数据来了。
可是,好纠结啊,要不要开新的一个app呢?
开一个新的EmoBox App 吧,这样整齐一点,把model import进来,应该就能render数据了。先试试。
建app 耶耶耶耶~
五部曲
部曲一
python3 manage.py startapp emobox
部曲二
setting.py 添加App
/workspace/how-do-you-do/hdyd/settings.py
部曲三 view.py 简单先建一个view,确保页面能显示后再进行下一步
/workspace/how-do-you-do/emobox/views.py
from django.shortcuts import render
# Create your views here.
def EmoBox(request):
return render(request, 'emobox/emobox.html')
部曲四 template,有了前端代码,用户才能看得见啊。
{% extends "base.html" %}
{% load static %}
{% block content %}
<!--Main Navigation-->
<header>
{% include 'mood/nav.html' %}
<!-- Background image -->
<div id="intro" class="p-5 text-center bg-image shadow-1-strong ">
<div class="mask bg-color ">
<div class="d-flex justify-content-center align-items-center h-100">
<div class="px-4 bg-light bg-gradient text-black-50">
<div class="my-4"></div>
<h4 class="border border-light my-4 p-4">
Check Your EmoBox
</h4>
<form action="" method="POST">
{% csrf_token %}
</form>
<button
type="button"
class="btn btn-outline-muted btn-lg m-2 text-black-50"
href="#"
role="button"
>
Stuffing
</button>
<div class="my-4"></div>
</div>
</div>
</div>
</div>
<!-- Background image -->
</header>
<hr class="m-0" />
{% include 'mood/footer.html' %}
{% endblock %}
部曲五, url,给个路径才知道去哪里看哦 ,这个是小app url
/workspace/how-do-you-do/emobox/urls.py
from django.contrib import admin
from django.urls import path
from . import views
urlpatterns = [
path('', views.EmoBox, name='emobox'),
]
大app也得包含进去,这样url才算设置完毕。
/workspace/how-do-you-do/hdyd/urls.py
回到主业HTML,加上新建的url,这样就能从主页传送过去EmoBox了。
<a class="btn btn-outline-muted btn-lg m-2 text-black-50" href="{% url 'emobox' %}" role="button">EmoBox</a>
初定完毕
/workspace/how-do-you-do/emobox/templates/emobox/emobox.html
下次就是连接database,把数据显示出来了。
跟大妈学Django Part - 2 (Django入门续集2)
跟大妈学Django Part - 3 (Django入门续集3)
跟大妈学Django Part - 4 (Django入门续集4)
跟大妈学Django Part - 5 (Django入门续集5)
跟大妈学Django Part - 6 (Django入门续集6)
跟大妈学Django Part - 7 (Django入门续集7)