我一直在阅读django文档,谷歌搜索没有与我相同的问题的日子。似乎“Views.py”中的方法“member_edit”没有返回任何数据,因此无法向我显示表单。
希望你们中的一些人能够发现我出错的地方。感谢我能得到的所有帮助!
Models.py
class Member(models.Model):
member_no = models.AutoField(primary_key=True)
first_name = models.CharField(max_length=50, null=True, blank=True)
last_name = models.CharField(max_length=50, null=True, blank=True)
email = models.CharField(max_length=50, null=True, blank=True)
reg_date = models.DateTimeField(null=True, blank=True)
class Meta:
db_table = 'Member'
Forms.py
class RegForm(forms.ModelForm):
first_name = forms.CharField(
widget=forms.TextInput(attrs={'class': 'form-control'}),
max_length=30,
required=True)
last_name = forms.CharField(
widget=forms.TextInput(attrs={'class': 'form-control'}),
max_length=30,
required=True)
email = forms.CharField(
widget=forms.EmailInput(attrs={'class': 'form-control'}),
required=True,
max_length=75)
reg_date = forms.DateField(widget=DateWidget(usel10n=True,bootstrap_version=3))
class Meta:
model = Member
exclude = ['last_login', 'date_joined']
fields = ['first_name', 'last_name', 'email', 'reg_date', ]
Views.py
def member_edit(request, member_no):
member = Member.objects.get(member_no=member_no)
if request.method == 'POST':
form = RegForm(request.POST or None, instance=member)
if form.is_valid():
member.first_name = form.request.POST['first_name']
member.last_name = form.request.POST['last_name']
member.email = form.request.POST['email']
member.reg_date = form.request.POST['reg_date']
member.save()
return redirect('member_overview')
return render(request, 'member/member_signup.html')
urls.py
urlpatterns = [
url(r'^member_edit/(?P<member_no>\d+)$', views.member_edit, name='member_edit')
]
member_edit.html
{% block body %}
<h1 class="logo"><a href="{% url 'member_overview' %}">Members</a></h1>
<div class="signup">
<h2>{% trans 'Update member' %}</h2>
<form action="{% url 'member_edit' member.member_no %}" method="post" role="form">
{% csrf_token %}
{% for field in form.visible_fields %}
<div class="form-group{% if field.errors %} has-error{% endif %}">
<label for="{{ field.label }}">{{ field.label }}</label>
<div class="col-sm-10">
{{ field }}
{% if field.help_text %}
<span class="help-block">{{ field.help_text }}</span>
{% endif %}
{% for error in field.errors %}
<label class="control-label">{{ error }}</label>
{% endfor %}
</div>
</div>
{% endfor %}
<center>
<button type="submit" class="btn btn-primary">{% trans 'Update member' %}</button>
</center>
</form>
</div>
{% endblock body %}
答案 0 :(得分:2)
您没有在上下文中传递表单(模板名称似乎不正确?):
def member_edit(request, member_no):
member = Member.objects.get(member_no=member_no)
if request.method == 'POST':
form = RegForm(request.POST or None, instance=member)
if form.is_valid():
# This isn't necessary - just save the form
#member.first_name = form.request.POST['first_name']
#member.last_name = form.request.POST['last_name']
#member.email = form.request.POST['email']
#member.reg_date = form.request.POST['reg_date']
#member.save()
form.save()
return redirect('member_overview')
# Instantiate the form
form = RegForm(instance=member)
# Add a dictionary for the context
return render(request, 'member/member_edit.html', {'form': form, 'member': member)
您可以使用a generic class based view:
使这更干净(更简单)from django.views import generic
from django.urls import reverse_lazy
class UpdateMember(generic.UpdateView):
model = Member
form_class = RegForm
pk_url_kwarg = 'member_no'
template_name = 'member/member_edit.html'
success_url = reverse_lazy('member_overview')
认为我获得了你需要的所有属性 - 你可以查看CCBV这是基于类的视图的一个很好的资源。
上的文档