我已经成功完成了ADDING阶段,但我确实陷入了编辑阶段。此功能将允许管理员通过浏览器更改特定成员的详细信息。有任何想法吗?这是我为ADDING所做的,但没有编辑。
视图
def edit_member(request, member_id):
context = RequestContext(request)
members = get_object_or_404(Member, id=member_id)
form = MemberForm(request.POST or None, instance=member)
if request.method == 'POST':
if form.is_valid():
form.save()
else:
form = MemberForm()
return render_to_response("templates/MemberUpdate.html",
{'members': members, 'form':form},
context)
Forms.py
class MemberForm(forms.ModelForm):
class Meta:
model = Member
MemberAdd.html
{% for member in members %}
<table>
<tr>
<td>{{ member.name}}</td>
</tr>
</table>
{% endfor %}
<p><h1>Add new members</h1></p>
<table>
<form action="" method="post">
<tr>
{% csrf_token %}
{{ form.as_p }}
</tr>
<input type="submit" value = "Submit" />
</form>
回溯:
Request Method: GET
Request URL: http://127.0.0.1:8000/
Django Version: 1.7.3
Python Version: 3.2.3
Installed Applications:
('django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'chess',
'member')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware')
Traceback:
File "/home/naveed/my_py3/lib/python3.2/site-packages/django/core/handlers/base.py" in get_response
98. resolver_match = resolver.resolve(request.path_info)
File "/home/naveed/my_py3/lib/python3.2/site-packages/django/core/urlresolvers.py" in resolve
343. for pattern in self.url_patterns:
File "/home/naveed/my_py3/lib/python3.2/site-packages/django/core/urlresolvers.py" in url_patterns
372. patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/home/naveed/my_py3/lib/python3.2/site-packages/django/core/urlresolvers.py" in urlconf_module
366. self._urlconf_module = import_module(self.urlconf_name)
File "/usr/lib/python3.2/importlib/__init__.py" in import_module
124. return _bootstrap._gcd_import(name[level:], package, level)
File "/usr/lib/python3.2/importlib/_bootstrap.py" in _gcd_import
821. loader.load_module(name)
File "/usr/lib/python3.2/importlib/_bootstrap.py" in load_module
436. return self._load_module(fullname)
File "/usr/lib/python3.2/importlib/_bootstrap.py" in decorated
141. return fxn(self, module, *args, **kwargs)
File "/usr/lib/python3.2/importlib/_bootstrap.py" in _load_module
342. exec(code_object, module.__dict__)
File "/home/naveed/my_py3/chess/chess/urls.py" in <module>
3. from member import views
File "/usr/lib/python3.2/importlib/_bootstrap.py" in load_module
436. return self._load_module(fullname)
File "/usr/lib/python3.2/importlib/_bootstrap.py" in decorated
141. return fxn(self, module, *args, **kwargs)
File "/usr/lib/python3.2/importlib/_bootstrap.py" in _load_module
330. code_object = self.get_code(name)
File "/usr/lib/python3.2/importlib/_bootstrap.py" in get_code
413. dont_inherit=True)
Exception Type: SystemError at /
Exception Value: ../Objects/tupleobject.c:126: bad argument to internal function
Urls.py
url(r'^$', views.index, name='index'),
url(r'profile', views.profile, name='profile'),
url(r'leaderboard', views.leaderboard, name='leaderboard'),
url(r'MemberForm', views.Form, name='MemberForm'),
url(r'MemberUpdate', views.MemberUpdate, name='MemberUpdate'),
url(r'^admin/', include(admin.site.urls)),
答案 0 :(得分:0)
你需要从这样的事情开始:
member = Member.objects.get(pk=member_id)
然后
form = MemberForm(request.POST, instance=member)
您的表单将预先填入成员属性。在POST时,将保存更改。
答案 1 :(得分:0)
如果您想要编辑特定的Member
实例,则需要从数据库中检索实例,然后将其传递给ModelForm
:
def edit_member(request, member_id):
member = get_object_or_404(Member, id=member_id)
form = MemberForm(request.POST or None, instance=member)
if request.method == 'POST':
if form.is_valid():
form.save()
# do something else, like redirect to a different view
return render(request, 'edit_member.html', {'form': form})
如果您想创建新成员:
def create_member(request):
form = MemberForm(request.POST or None)
if request.method == 'POST':
if form.is_valid():
form.save()
# do something else, like redirect to a different view
return render(request, 'add_member.html', {'form': form})
由于您可以在同一视图中实际完成创建和编辑操作,所以非常啰嗦,但我想展示每个操作中发生的事情。
除非您需要从视图中添加非常具体的内容,否则没有理由手动从已清理的数据中设置模型字段,因为表单已经完成了致电.is_valid()
此外,您还需要正确形成您的网址格式,因为它们需要正确的正则表达式:
url(r'^$', views.index, name='index'),
url(r'^profile/$', views.profile, name='profile'),
url(r'^leaderboard/$', views.leaderboard, name='leaderboard'),
url(r'^MemberForm/$', views.Form, name='MemberForm'),
url(r'^MemberUpdate/(?P<member_id>\d+)/$',
views.MemberUpdate, name='MemberUpdate'),
url(r'^admin/', include(admin.site.urls))
并请在Python中使用方法,属性和函数的小写名称和类的大写名称。
最后,您需要更新模板代码以传递member_id
:
{% for member in members %}
<table>
<tr>
<td><a href="{% url 'MemberUpdate' member_id=member.id %}">{{ member.name}}</a></td>
</tr>
</table>
{% endfor %}