我对Django很陌生,我花了好几个小时尝试可能的解决方案。
我的观点需要一个小兵:
def inventory(request, id):
model = Inventory
# thing = Inventory.objects.get(pk=id)
if request.method == "POST":
stuff = Inventory.objects.get(id=id)
form = InventoryForm(data = request.POST, instance=stuff)
if form.is_valid():
# if Inventory.filter(item='item').exists()
# obj = form.save(commit=False)
form.save()
else:
stuff = Inventory.objects.get(id=id)
form = InventoryForm(instance=stuff)
context = RequestContext(request)
return render_to_response('inventory.html', {
'form':form, 'stuff':stuff
}, RequestContext(request))
当我尝试以下网址格式时:
url(r'^inventory/(?P<id>\d+)/$', inventory, kwargs=dict(model=models.Inventory) name ='inventory'),
我得到了404。
此网址格式:
url(r'^inventory/$', inventory, name='inventory'),
给我以下错误:
inventory()只需要2个参数(给定1个)
这里的语法是什么?
编辑添加此网址正常工作,直到我添加了允许用户编辑的代码以及添加内容。
我在尝试以下建议时遇到的新错误的ETA追溯:
Environment:
Request Method: GET
Request URL: http://localhost:8000/inventory/16/
Django Version: 1.8
Python Version: 2.7.6
Installed Applications:
('django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django_tables2',
'camp')
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',
'django.middleware.security.SecurityMiddleware')
Template error:
In template /Users/rebeccaeliserobbins/Documents/repos/djangoBurnApp/bioluminati/camp/templates/base.html, error at line 58
Reverse for 'inventory' with arguments '()' and keyword arguments '{}' not found. 1 pattern(s) tried: ['inventory/(?P<id>\\d+)/$']
48 : </ul>
49 : </li>
50 : <li><a href="#">Setup and Exedus</a>
51 : <ul>
52 : <li><a href="#">Sign up for shifts</a></li>
53 : <li><a href="#">Instructions and plans for setup</a></li>
54 : </ul>
55 : </li>
56 : <li><a href="#">Operations</a>
57 : <ul>
58 : <li><a href= {% url 'inventory' %} >Truck Inventory</a></li>
59 : <li><a href="#">Budget </a></li>
60 : </ul>
61 : </li>
62 :
63 : {%else %}
64 : <li><a href="login">Login</a></li>
65 : <li><a href="#">Contact</a></li>
66 : <li><a href="#">Join us</a></li>
67 : {% endif %}
68 : <li><a href="#">Resources</a>
Traceback:
File "/Users/rebeccaeliserobbins/Documents/repos/djangoBurnApp/env/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
132. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/rebeccaeliserobbins/Documents/repos/djangoBurnApp/bioluminati/camp/views.py" in inventory
182. return render(request, 'inventory.html', {'form': form, 'stuff': stuff})
File "/Users/rebeccaeliserobbins/Documents/repos/djangoBurnApp/env/lib/python2.7/site-packages/django/shortcuts.py" in render
67. template_name, context, request=request, using=using)
File "/Users/rebeccaeliserobbins/Documents/repos/djangoBurnApp/env/lib/python2.7/site-packages/django/template/loader.py" in render_to_string
99. return template.render(context, request)
File "/Users/rebeccaeliserobbins/Documents/repos/djangoBurnApp/env/lib/python2.7/site-packages/django/template/backends/django.py" in render
74. return self.template.render(context)
File "/Users/rebeccaeliserobbins/Documents/repos/djangoBurnApp/env/lib/python2.7/site-packages/django/template/base.py" in render
209. return self._render(context)
File "/Users/rebeccaeliserobbins/Documents/repos/djangoBurnApp/env/lib/python2.7/site-packages/django/template/base.py" in _render
201. return self.nodelist.render(context)
File "/Users/rebeccaeliserobbins/Documents/repos/djangoBurnApp/env/lib/python2.7/site-packages/django/template/base.py" in render
903. bit = self.render_node(node, context)
File "/Users/rebeccaeliserobbins/Documents/repos/djangoBurnApp/env/lib/python2.7/site-packages/django/template/debug.py" in render_node
79. return node.render(context)
File "/Users/rebeccaeliserobbins/Documents/repos/djangoBurnApp/env/lib/python2.7/site-packages/django/template/loader_tags.py" in render
135. return compiled_parent._render(context)
File "/Users/rebeccaeliserobbins/Documents/repos/djangoBurnApp/env/lib/python2.7/site-packages/django/template/base.py" in _render
201. return self.nodelist.render(context)
File "/Users/rebeccaeliserobbins/Documents/repos/djangoBurnApp/env/lib/python2.7/site-packages/django/template/base.py" in render
903. bit = self.render_node(node, context)
File "/Users/rebeccaeliserobbins/Documents/repos/djangoBurnApp/env/lib/python2.7/site-packages/django/template/debug.py" in render_node
79. return node.render(context)
File "/Users/rebeccaeliserobbins/Documents/repos/djangoBurnApp/env/lib/python2.7/site-packages/django/template/defaulttags.py" in render
329. return nodelist.render(context)
File "/Users/rebeccaeliserobbins/Documents/repos/djangoBurnApp/env/lib/python2.7/site-packages/django/template/base.py" in render
903. bit = self.render_node(node, context)
File "/Users/rebeccaeliserobbins/Documents/repos/djangoBurnApp/env/lib/python2.7/site-packages/django/template/debug.py" in render_node
79. return node.render(context)
File "/Users/rebeccaeliserobbins/Documents/repos/djangoBurnApp/env/lib/python2.7/site-packages/django/template/defaulttags.py" in render
507. six.reraise(*exc_info)
File "/Users/rebeccaeliserobbins/Documents/repos/djangoBurnApp/env/lib/python2.7/site-packages/django/template/defaulttags.py" in render
493. url = reverse(view_name, args=args, kwargs=kwargs, current_app=current_app)
File "/Users/rebeccaeliserobbins/Documents/repos/djangoBurnApp/env/lib/python2.7/site-packages/django/core/urlresolvers.py" in reverse
579. return force_text(iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs)))
File "/Users/rebeccaeliserobbins/Documents/repos/djangoBurnApp/env/lib/python2.7/site-packages/django/core/urlresolvers.py" in _reverse_with_prefix
496. (lookup_view_s, args, kwargs, len(patterns), patterns))
Exception Type: NoReverseMatch at /inventory/16/
Exception Value: Reverse for 'inventory' with arguments '()' and keyword arguments '{}' not found. 1 pattern(s) tried: ['inventory/(?P<id>\\d+)/$'
答案 0 :(得分:3)
此模式:url(r'^inventory/(?P<id>\d+)/$', inventory, kwargs=dict(model=models.Inventory) name ='inventory'),
显示404,因为您需要使用存在的对象ID转到该网址 - 例如<site>.com/inventory/1
您尝试的第二种方式不起作用,因为url(r'^inventory/$', inventory, name='inventory'),
无法捕获id
参数(通过(?P<id>\d+)
),但在您的视图中可以使用def inventory(request, id):
)。
所以这是适当的返工,我也为你清理了一些代码:
views.py
from django.shortcuts import render
def inventory(request, id):
if request.method == "GET":
stuff = Inventory.objects.get(id=id)
form = InventoryForm(instance=stuff)
if request.method == "POST":
stuff = Inventory.objects.get(id=id)
form = InventoryForm(data=request.POST, instance=stuff)
if form.is_valid():
form.save()
return render(request, 'inventory.html', {'form': form, 'stuff': stuff})
urls.py
url(r'^inventory/(?P<id>\d+)/$', inventory, name='inventory'),