我试图使用"编辑"来获取预先填充的表单。我在桌子上使用的按钮。我已经尝试了一切,但我无法获得预先填充的表格。我只使用一个表单来添加和编辑。
这是我的views.py编辑功能:
def edit_row(request, rowid):
item = get_object_or_404(Studentapp, id=rowid)
print item
if request.method=="POST":
form = EntryForm(request.POST, instance=item)
if form.is_valid():
post=form.save(commit=False)
post.save()
return HttpResponseRedirect(reverse('studentapp:index'),rowid.id)
else:
form=EntryForm(instance=item)
return render(request, 'index.html',{'form':form})
else:
form=EntryForm(instance=item)
return render(request, 'index.html',{'form':form})
这是我使用的形式:
<div class="modal-dialog">
<div class = "modal-content">
<div class = "modal-header">
<button type = "button" class = "close" data-dismiss="modal">×</button>
<h3 class="modal-title"><b>Add Student</b></h3>
</div>
<div class = "modal-body">
<form action = "{% url 'studentapp:addstudent' %}" id="addform" method = "POST">
{% csrf_token %}
<div class = "form-group">
<label for = "your_name">Your name: </label>
<input class = "form-control" id="new_name" type = "text" name="name" value="{{ current_name }}" placeholder="Enter your name">
</div>
<div class="form-group">
<label for = "course_name">Course: </label>
<input id="new_course" class = 'form-control' type = "text" name="course" value="{{ current_course }}" placeholder="Enter your course">
</div>
<div class = "form-group">
<label for = "rollno">Roll No.: </label>
<input id="new_rollno" type = "text" class = 'form-control' name="roll" value="{{ current_roll }}" placeholder="Enter your roll number">
</div>
<div class = "form-group">
<label for ="addr">Address: </label>
<textarea id="new_address" type = "text" name="address" class = 'form-control' value="{{ current_addr }}" placeholder="Enter your address"></textarea>
</div>
<input type = "submit" value="Submit" class = "btn btn-success" style="font-size:18px;" />
</form>
</div>
</div>
</div>
这是我的forms.py:
class EntryForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(EntryForm, self).__init__(*args, **kwargs)
self.fields['name'].widget.attrs.update(
{'class': 'form-control', 'placeholder': 'Enter your name'})
self.fields['roll'].widget.attrs.update(
{'class': 'form-control', 'placeholder': 'Enter your roll'})
self.fields['course'].widget.attrs.update(
{'class': 'form-control', 'placeholder': 'Enter your course'})
self.fields['address'].widget.attrs.update(
{'class': 'form-control', 'placeholder': 'Enter your address'})
class Meta:
model = Studentapp
fields = ['name','roll','course','address']
答案 0 :(得分:0)
在您的视图中,当请求方法为GET
时,传递填充的表单。
def edit_row(request, rowid):
item = get_object_or_404(Studentapp, id=rowid)
print item
if request.method=="POST":
form = EntryForm(request.POST, instance=item)
if form.is_valid():
post=form.save(commit=False)
post.save()
return HttpResponseRedirect(reverse('studentapp:index'),rowid.id)
else:
form=EntryForm(instance=item)
return render(request, 'index.html',{'form':form})
else:
form=EntryForm(instance=item)
return render(request, 'index.html',{'form':form})
在您的模板中,不要手动放置所有行,而是使用表单。
{{ form.as_p }}
如果要自定义样式,可以在表单上进行迭代,并在使用自己的设计时获取字段以显示它们。
<form action="" id="addform" method="POST">
{% csrf_token %}
{% for f in form %}
<div class = "form-group">
<label class="form-control-label">{{ f.name }}</label>
<div>{{ f }}</div>
</div>
<input type = "submit" value="Submit" class = "btn btn-success" style="font-size:18px;" />
</form>
并更新表单以包含您自己的风格。
from django import forms
from studentapp.models import Studentapp
class EntryForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(EntryForm, self).__init__(*args, **kwargs)
self.fields['name'].widget.attrs.update(
{'class': 'form-control', 'placeholder': 'Enter your name'})
self.fields['roll'].widget.attrs.update(
{'class': 'form-control', 'placeholder': 'Enter your roll'})
self.fields['course'].widget.attrs.update(
{'class': 'form-control', 'placeholder': 'Enter your course'})
self.fields['address'].widget.attrs.update(
{'class': 'form-control', 'placeholder': 'Enter your address'})
class Meta:
model = Studentapp
fields = ['name','roll','course','address']
答案 1 :(得分:0)
@MD。 Khairul Basar的答案是正确的方法,但是如果由于某种原因你想手动放置所有行,HTML输入元素的name属性应该与字段名称匹配,请参阅下面的更改:
<div class="modal fade" id="addform" role="dialog">
<div class="modal-dialog">
<div class = "modal-content">
<div class = "modal-header">
<button type = "button" class = "close" data-dismiss="modal">×</button>
<h3 class="modal-title"><b>Add Student</b></h3>
</div>
<div class = "modal-body">
<form action = "{% url 'studentapp:addstudent' %}" id="addform" method = "POST">
{% csrf_token %}
<div class = "form-group">
<label for = "your_name">Your name: </label>
<input class = "form-control" id="new_name" type = "text" name="name" value="{{ current_name }}" placeholder="Enter your name">
</div>
<div class="form-group">
<label for = "course_name">Course: </label>
<input id="new_course" class = 'form-control' type = "text" name="course" value="{{ current_course }}" placeholder="Enter your course">
</div>
<div class = "form-group">
<label for = "rollno">Roll No.: </label>
<input id="new_rollno" type = "text" class = 'form-control' name="roll" value="{{ current_roll }}" placeholder="Enter your roll number">
</div>
<div class = "form-group">
<label for ="addr">Address: </label>
<textarea id="new_address" type = "text" name="address" class = 'form-control' value="{{ current_addr }}" placeholder="Enter your address"></textarea>
</div>
<input type = "submit" value="Submit" class = "btn btn-success" style="font-size:18px;" />
</form>
</div>
</div>
</div>