我正在尝试从数据库中预先填充字段。 我有一个html页面,并且有一个表格,我将在该表格上单击链接,该链接将使用从数据库填充的值来呈现html表单。
但是问题是:我无法预填充HTML表单字段。 以下是我的代码,我无法获取输出。
表中的html代码
<td>{{ info.university_code }}</td>
<td>{{ info.avg_grescore }}</td>
<td>{{ info.avg_english_score }}</td>
<td>{{ info.avg_undergradcgpa }} CGPA</td>
<td>{{ info.avg_workex_months }} Months</td>
<td>{{ info.avg_research_skills }}%</td>
<td>{{ info.avg_acceptancepercentage }}</td>
<td><a href="{% url 'editSummary' univ_code=info.university_code %}"
Views.py(这里存在我所有的逻辑,这不起作用并给我一个错误)
def editSummary(request,univ_code):
if request.method == "POST":
form = UniversitySummaryForm(request.POST or None, instance =
uiversity)
if form.is_valid():
form.save()
return redirect('/AdminHome')
else:
form = UniversityInfoForm()
form.fields["university_code"].queryset =UniversityNames.objects.filter(university_code=request.university_code)
template_vars = RequestContext(request, {
"form": form,
"university_code": request.university_code
})
return render_to_response(
"PredictiveAcceptance/AdminUniversitySummary.html",
template_vars)
Models.py(用于数据库字段)
class UniversitySummaries(models.Model):
university_code = models.ForeignKey('UniversityNames',
models.DO_NOTHING, db_column='university_code', primary_key=True)
avg_grescore = models.IntegerField()
avg_english_score = models.IntegerField()
avg_undergradcgpa = models.IntegerField()
avg_workex_months = models.IntegerField()
avg_research_skills = models.IntegerField()
avg_acceptancepercentage = models.IntegerField()
created_on = models.DateTimeField(auto_now_add = True)
Forms.py文件
class UniversitySummaryForm(forms.ModelForm):
class Meta:
model = UniversitySummaries
fields = ('university_code', 'avg_grescore',
'avg_english_score', 'avg_undergradcgpa', 'avg_workex_months',
'avg_research_skills', 'avg_acceptancepercentage')
urls.py
urlpatterns = [
url(r'^AdminHome/$', AdminSiteViews.home, name='home'),
url(r'^AdminListOfUniversity/$', AdminSiteViews.list, name='list'),
url(r'^AdminUniversitySummary/$', AdminSiteViews.summary, name='summary'),
url(r'^editSummary/<univ_code>/$', AdminSiteViews.editSummary, name='editSummary'),
]
答案 0 :(得分:0)
该错误与表单本身无关。而是,模板包含带有解析程序调用的URL标记,这将失败。解析器无法为此标签创建URL:
<a href="{% url 'editSummary' univ_code=info.university_code %}"
您在这里使用外键的对象表示作为univ_code
的值,但是info.university_code
是UniversityNames
类型的对象的字符串表示。
university_code = models.ForeignKey('UniversityNames',
models.DO_NOTHING, db_column='university_code', primary_key=True)
您尚未发布模型,但我想url标签应如下所示:
<a href="{% url 'editSummary' univ_code=info.university_code.university_code %}"
从代码中的这一行推导出来的:
UniversityNames.objects.filter(university_code=request.university_code)
答案 1 :(得分:0)
模板中没有称为info
的内容,因此不能将info.university_code
用作url
标签的参数。
您可以只做{% url 'editSummary' univ_code=university_code %}
来解决它,但是模板的其余部分仍然会损坏。