Resumen de las lecciones de Django de hoy: modelo, vista y concatenación de plantillas-20210325

Hoy habilité con éxito mi modelo para que se muestre en la plantilla. La secuencia es la siguiente:
1. Construye el modelo y soluciona el problema de establecimiento del modelo.
Error de Django al ejecutar el archivo de migración, es decir, cuando ingresas'Python manger. py 'en la Terminal bajo Pycharm Se produjo el siguiente error durante las migraciones':

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
  • Solución:
    elimine el archivo 0001_initial.py en migraciones y luego vuelva a ejecutar python manage.py makemigrations y python manage.py migrate en Terminal para regenerar el archivo de migración.

2. Utilice la API para obtener datos por adelantado. Luego, aplíquelo a vistas y plantillas.

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. Configure la vista myclass de la siguiente manera:
Preste atención a 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. Configure la URL de la siguiente manera:

  • URL en el proyecto training_system
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')),
]
  • URL en el proyecto myclass
from django.urls import path
from . import views

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

5. Configuración de plantillas Se
recomienda configurar plantillas en el proyecto training_system, de la siguiente manera:
Preste atención a 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>

Inserte la descripción de la imagen aquí
El formulario de front-end html puede referirse a: https://blog.csdn.net/fanqingtulv/article/details/81865394?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522161667978816780269866130%2522%252C%2522s % 253A% 252220140713.130102334…% 2522% 257D & request_id = 161667978816780269866130 & biz_id = 0 & utm_medium = distrib.pc_search_result.none-task-blog-2 all sobaiduend ~ default-1-8186539_2% E4% E% E% BB0% E% BB0% E4% E% E% BB0 A0% 81

Supongo que te gusta

Origin blog.csdn.net/m0_46629123/article/details/115218730
Recomendado
Clasificación