Html Django表单未显示在模板中
为什么表单不显示在浏览器中 index.htmlHtml Django表单未显示在模板中,html,css,django,templates,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><stron
<!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>
另外,我还不太明白这一点,因为我正在使用NewBoston频道的视频教程系列,我在addRecord中使用的变量未知,我想从模型中使用它们。以下是模型和URL文件:
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
URL.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'),
]
从django.conf.url导入url
从…起导入视图
应用程序名称='结构'
URL模式=[
#/结构/
url(r'^$',views.index,name='index'),
#/structures/id
url(r'^(?P[0-9]+)/$',views.detail,name='detail'),
#/structures/addrecord
url(r'^(?P[0-9]+)/addrecord/$',views.addrecord,name='addrecord'),
]
查找从视图在上下文中传递的所有\u结构的值
如果为空或未传递,则表单字段将不会按照模板代码显示
尝试在控制台或模板中打印其值。这就是调试的方法
希望这有帮助。显示视图。什么是
所有\u结构
,它是否包含任何内容?record
来自哪里?我添加了您需要的所有代码。这不是真正的代码;您的addRecord
定义包含非法语法。非真实代码是什么意思?还有什么非法语法?顺便说一句,变量(record.name、record.type、record.pos、record.long)应该是(record\u name、record\u type、record\u pos、record\u long)。我已经纠正了它们,只是我给了你一个稍微旧的代码。它不是空的,当我转到索引模板时,我可以看到2个结构。
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'),
]