Django表单未显示在模板

时间:2017-10-26 11:05:32

标签: html css django templates

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

的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。 我甚至明确地给了一个样式=&#34;身高:500px&#34;,但它仍然给我一个空的形式。

这里是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中使用的变量是未知的,我想从模型中使用它们。以下是模型和网址文件:

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'),
]

1 个答案:

答案 0 :(得分:0)

查看您在视图中传递的 all_structures 的值。

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

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

希望这有帮助。