简体   繁体   English

如何从 Django 模型中获取数据并输入到 HTML 模板中?

[英]How to get data out of a Django Model and in to the HTML Template?

I'm trying to display information from my model Administrateur into my HTML Template login.html , but nothing is happening.我正在尝试login.html自我的模型Administrateur信息显示到我的 HTML 模板login.html ,但没有任何反应。

This my Model :这是我的模型

from django.db import models

class Administrateur(models.Model):
    nom = models.CharField(max_length=30)
    prenom = models.CharField(max_length=30)
    mdp = models.CharField(max_length=30)
    mail = models.CharField(max_length=30)

    def _str_(self):
        return self.name

This is my view :这是我的观点

def Pseudo(request):
    administrateurs = Administrateur.objects.all()
    context={'administrateurs':administrateurs}
    return render(request, "login.html",context)

This is my HTML :这是我的 HTML

{% extends "base.html" %}
{% load static %}
{% block content %}
{% for n in administrateurs %}
{{ n.nom }}
{{ n.prenom }}
{% endfor %}
{% endblock %}

I'm not sure what to do with urls.py我不确定如何处理 urls.py

Do this step if you have not done it yet: Add this to the file urls.py in the project's base directory:如果您还没有这样做,请执行此步骤:将其添加到项目基目录中的文件 urls.py 中:

from django.urls import include
urlpatterns = [
    # your urls
    path('login/', include('appname.urls')), #change appname to your app's name
]

now in your app make a new file called urls.py and add this to the file:现在在您的应用程序中创建一个名为 urls.py 的新文件并将其添加到文件中:

from django.urls import path
from .views import Pseudo

urlpatterns = [
    path('', Pseudo, name="pseudo"),
]

then check when you go to login page (in url bar) what happens然后检查您何时进入登录页面(在 url 栏中)会发生什么

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM