iT邦幫忙

2021 iThome 鐵人賽

DAY 10
0
Modern Web

Python x Django 網站實作&學習記錄系列 第 10

D10 doc系統、首頁

製作文件系統首頁
先將使用者的個人資料頁做好
回到docsystem_5

django-admin startapp doc_info

創建新的app

doc_info/models.py 先建立好資料庫的欄位

from django.db import models
from django.contrib.auth.models import User
from allauth.account.models import EmailAddress

class doc_warehouse(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='doc_warehouse')
    create_date = models.DateTimeField(auto_now_add=True)
    last_mod_date = models.DateTimeField(auto_now=True)
    title = models.CharField(max_length=128, blank=True)
    remark = models.CharField(max_length=128, blank=True)
    status = models.IntegerField(default=1, null=True, blank=True)
    link = models.CharField(max_length=128, blank=True)

doc_info/views.py 設定邏輯
把所有在文件倉庫的資料傳給templates

from django.shortcuts import render, get_object_or_404
from auth_info.models import UserProfile
from .models import doc_warehouse
from django.http import HttpResponseRedirect
from django.urls import reverse
from django.contrib.auth.decorators import login_required

def doc_main(request):
    user = request.user
    Doc_warehouse = doc_warehouse.objects.all()
    return render(request, 'doc/main.html', {'Doc_warehouse': Doc_warehouse})

tmplates/doc/main.html 設定首頁長相
首先先列出使用者個人資料頁、個人文件頁、登入登出按鈕
然後直接列出所有檔案

{% block content %}

<a href="{% url 'auth_info:profile' %}">My Profile</a> | 
<a href="{% url 'doc_info:user_list' %}">My Documents</a> |
{% if user.is_authenticated %}
<a href="{% url 'account_logout' %}">Logout</a>
{% else %}
<a href="{% url 'account_login' %}">Login</a>
{% endif %}

<p>Welcome {{ user.first_name }} {{ user.last_name }}</p>

<h2>Document List</h2>
<table border = "1">
    <thead>
        <tr>
            <th >Create date: </th>
            <th>Last modified date:</th>
            <th>Author</th>
            <th>Title</th>
            <th>Remark</th>
            <th>Link</th>
        </tr>
    </thead>
    <tbody>
     {% for doc in Doc_warehouse %}
        <tr>
            <td>{{ doc.create_date }}</td>
            <td>{{ doc.last_mod_date }}</td>
            <td>{{ doc.user }}</td>
            <td>{{ doc.title }}</td>
            <td>{{ doc.remark }}</td>
            <td>{{ doc.link }}</td>
     {% endfor %}
        </tr>
    </tbody>
  </table>

{% endblock %}

doc_info/urls.py 設定轉址規則

from django.urls import path
from . import views

app_name = "doc_info"
urlpatterns = [
    path('doc/main/', views.doc_main, name='main'),
]

docsystem_5/setting.py 加入app

INSTALLED_APPS = [
    'doc_info',
]

記得要執行以下創建table

python manage.py makemigrations #告訴django依據model跟installed_app要該棟那些table
python manage.py migrate        #執行以上的變動
python manage.py runserver      #執行server

http://127.0.0.1:8000/doc/main/ 展示如下圖

Imgur


上一篇
D9 文件系統核心開始 系統頁面功能規劃
下一篇
D11 新增測試頁
系列文
Python x Django 網站實作&學習記錄30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言