【问题标题】:Django form not showing in template模板中未显示 Django 表单
【发布时间】:2018-04-07 17:43:50
【问题描述】:

为什么表单没有显示在浏览器中?

index.html

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>{{ structure.name }} - Details</title>
  </head>
  <body>
    {% if error_message %}
    <p><strong>{{ error_message }}</strong></p>
    {% endif %}

    <h3>Structure: {{ structure }}</h3>

    <h3>Ajouter enregistrement</h3>
    <form action="{% url 'Structure:addrecord' structure.id %}" method="post">
      {% csrf_token %}
      {% for structure in all_structures %}
        <input type="radio" id="record{{ forloop.counter }}" name="record" value="record.id">
        <label for="record{{ forloop.counter }}">
          Nom de l'enregistrement: {{ record.record_name }}
        </label>
      {% endfor %}
    </form>
  </body>
</html>

当我在浏览器中测试并检查它时,它给了我一个这样大小的表格:1340px * 0 px。 我什至明确给出了 style="height: 500px",但它仍然给了我一个空表单。

这是screenshot

感谢大家的帮助!

编辑:

Views.py:

from django.shortcuts import render, get_object_or_404
from .models import Structure, Record

def index(request):
    all_structures = Structure.objects.all()
    return render(request, 'Structures/index.html', {'all_structures': all_structures})

def detail(request, structure_id):
    #structure = Structure.objects.get(pk=structure_id)
    structure = get_object_or_404(Structure, pk=structure_id)
    return render(request, 'Structures/details.html', {'structure': structure})

def addRecord(request, structure_id, record.name, record.type, record.pos, record.long):
    r = Record(structure='structure_id', name='record.name', type='record.type', pos='str(record.pos)', long='str(record.long)')
    r.save()
    return render(request, 'Structures/details.html', {'structure': structure})

另外,我还不太明白这一点,因为我正在使用 newboston 频道的视频教程系列,我在 addRecord 中使用的变量是未知的,我想从模型中使用它们。以下是模型和 urls 文件:

models.py:

from django.db import models

class Structure(models.Model):
    name = models.CharField(max_length=120)
    path = models.CharField(max_length=200)

    def __str__(self):
        return self.name

class Type(models.Model):
    typename = models.CharField(max_length=50)

    def __str__(self):
        return self.typename

class Record(models.Model):
    structure = models.ForeignKey(Structure, on_delete=models.CASCADE) #each structure has many records, each per line
    name = models.CharField(max_length=200)
    type = models.ForeignKey(Type)
    pos = models.IntegerField()
    long = models.IntegerField()

    def __str__(self):
        return self.name

urls.py:

from django.conf.urls import url
from . import views

app_name = 'Structure'

urlpatterns = [
    # /structures/
    url(r'^$', views.index, name='index'),

    # /structures/id
    url(r'^(?P<structure_id>[0-9]+)/$', views.detail, name='detail'),

    # /structures/addrecord
    url(r'^(?P<structure_id>[0-9]+)/addrecord/$', views.addRecord, name='addrecord'),
]

【问题讨论】:

  • 显示视图。 all_structures 是什么,它包含任何内容吗? record 来自哪里?
  • 我添加了你需要的所有代码。
  • 这不是真正的代码;您的 addRecord 定义包含非法语法。
  • 不是真正的代码是什么意思?什么非法语法?
  • 顺便说一句,变量(record.name、record.type、record.pos、record.long)应该是(record_name、record_type、record_pos、record_long)。我已经更正了它们只是我给了你一个稍微旧的代码。

标签: html css django templates


【解决方案1】:

查找您在视图上下文中传递的 all_structures 的值。

如果为空或未通过,表单字段将不会按照您的模板代码显示。

尝试在控制台或模板中打印其值。这就是调试的方法。

希望这会有所帮助。

【讨论】:

  • 它不是空的,当我转到索引模板时,我可以看到2个结构。
猜你喜欢
  • 2014-08-12
  • 2017-10-20
  • 2018-08-21
  • 2017-12-15
  • 2021-04-01
  • 2023-01-10
  • 2021-09-06
  • 1970-01-01
相关资源
最近更新 更多