今日のDjangoレッスンの概要:モデル、ビュー、テンプレートの連結-20210325

今日、モデルをテンプレートに表示できるようになりました。シーケンスは次のとおりです
。1。モデルをビルドし、モデル確立の問題を解決します。
移行ファイルの実行時、つまり「Pythonマネージャー」と入力したときのDjangoのエラー。 Pycharmの下のターミナルのpy'makemigrations '中に次のエラーが発生しました:

You are trying to add a non-nullable field 'hbook' to heroinfo without a default;
 we can't do that (the database needs something to populate existing rows).
Please select a fix:
 1) Provide a one-off default now (will be set on all existing rows with a null v
alue for this column)
 2) Quit, and let me add a default in models.py
  • 解決策:
    移行中の0001_initial.pyファイルを削除してから、ターミナルでpython manage.pymakemigrationsとpythonmanage.pymigrateを再実行して移行ファイルを再生成します。

2.APIを使用して事前にデータを取得します。次に、それをビューとテンプレートに適用します。

C:\Users\Season\workspace2\training_system>python manage.py shell
Python 3.7.4 (tags/v3.7.4:e09359112e, Jul  8 2019, 20:34:20) [MSC v.1916 64 bit (AMD64)]
Type 'copyright', 'credits' or 'license' for more information
IPython 7.14.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: from myclass.models import Subject

In [2]: Subject.objects.all()
Out[2]: <QuerySet [<Subject: HR-001.培训>, <Subject: HR-002.招聘>]>

In [3]: s = Subject..objects.filter(id=1)
  File "<ipython-input-3-3fdf41ec21fe>", line 1
    s = Subject..objects.filter(id=1)
                ^
SyntaxError: invalid syntax


In [4]: s = Subject.objects.filter(id=1)

In [5]: s
Out[5]: <QuerySet []>

In [6]: s = Subject.objects.filter(Subject_id='HR-001')

In [7]: s
Out[7]: <QuerySet [<Subject: HR-001.培训>]>
 


3.myclassビューを次のように設定します。Subject_listに注意してください。

from django.shortcuts import render
from .models import Subject,Employee,Employee_Subject,Teacher

# Create your views here.

def index(request):
    Subject_list = Subject.objects.order_by('-Subject_id').all()[:5]
    context = {
    
    'Subject_list':Subject_list}
    return render(request,'myclass/index.html',context=context)

4.URLを次のように設定します。

  • training_systemプロジェクトのURL
from django.contrib import admin
from django.urls import path,include
from myclass import views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('index/', views.index, name='index'),
    path('myclass/',include('myclass.urls')),
]
  • myclassプロジェクトのURL
from django.urls import path
from . import views

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

5.テンプレートの設定
次のように、training_systemプロジェクトでテンプレートを設定することをお勧めします
。Subject_listに注意してください。

<html>
<head>
<title>notice</title>
</head>

<body>
<table>

    {% for item in Subject_list %}
    <tr>
    <td>{
   
   { item.Subject_id }}</td>
    <td>{
   
   { item.subject_name }}</td>
    <td>{
   
   { item.teacher.name }}</td>
    </tr>
    {% endfor %}

</table>
</body>

ここに画像の説明を挿入
フロントエンドフォームのhtmlは、https//blog.csdn.net/fanqingtulv/article/details/81865394? ops_request_misc =%257B%2522request%255Fid%2522%253A%2522161667978816780269866130%2522%252C%2522scm%2522を参照できます。 %253A%252220140713.130102334…%2522%257D&request_id = 161667978816780269866130&biz_id = 0&utm_medium = distribution.pc_search_result.none-task-blog-2 all sobaiduend〜default-1-81865394.first_rank_v2_pc_rank_v29&utm_term =%E8%E4%E8%E4 A0%81

おすすめ

転載: blog.csdn.net/m0_46629123/article/details/115218730