为什么URL没有将信息传递给我的视图?

时间:2018-03-30 18:08:38

标签: python django django-forms django-views

我正在尝试配置urls.py以通过URL传递信息。

当url匹配“requests /”时,我的主urls.py文件包含以下requests / urls.py

完整请求/ urls.py:

from django.urls import path
from django.conf.urls import include, url
from . import views


urlpatterns = [
    path('admin/<id>/', views.RequestQueue),
    url('admin/', views.RequestQueue, name="queue"),
    url('', views.ps_request, name='requests'),
]
来自views.py的

    def RequestQueue(request, id=0):

    #handle updates to request status
    if request.method == "POST":
        request_update = Request.objects.get(pk=id)
        form = RequestComplete(request.POST, instance=request_update)
        form.save()

    pending_requests = Request.objects.filter(status='pending')
    context = {'pending_requests' : pending_requests, 'status_form' : RequestComplete}
    template_name = 'pending-queue.html'
    return render(request, template_name, context)

当我使用url运行开发服务器时:

http://127.0.0.1:8000/requests/admin/10/

“id”的值仍然等于默认值.. 0

它不应该更新为URL中的数字值吗?

编辑 - 追溯:     环境:

Request Method: POST
Request URL: http://127.0.0.1:8000/requests/admin/7

Django Version: 2.0.3
Python Version: 3.6.4
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'aboutme',
 'contactme',
 'experience',
 'login',
 'psrequest']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware'] 



Traceback:

File "C:\Users\name\PycharmProjects\user\venv\lib\site-packages\django\core\handlers\exception.py" in inner
  35.             response = get_response(request)

File "C:\Users\name\PycharmProjects\user\venv\lib\site-packages\django\core\handlers\base.py" in _get_response
  128.                 response = self.process_exception_by_middleware(e, request)

File "C:\Users\name\PycharmProjects\user\venv\lib\site-packages\django\core\handlers\base.py" in _get_response
  126.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "C:\Users\name\python\name\psrequest\views.py" in RequestQueue
  31.         request_update = Request.objects.get(pk=id)

File "C:\Users\name\PycharmProjects\user\venv\lib\site-packages\django\db\models\manager.py" in manager_method
  82.                 return getattr(self.get_queryset(), name)(*args, **kwargs)

    File "C:\Users\name\PycharmProjects\user\venv\lib\site-packages\django\db\models\query.py" in get
  403.                 self.model._meta.object_name

Exception Type: DoesNotExist at /requests/admin/7
Exception Value: Request matching query does not exist.

1 个答案:

答案 0 :(得分:0)

您的第一个网址格式为:

path('admin/<id>/', views.RequestQueue),

这意味着它只会处理admin/<id>/的请求。尾部斜杠很重要 - 它与admin/<id>不匹配。

下一个问题是您错误地使用了url()。当您使用url()时,它会使用正则表达式,因此您需要^$来标记字符串的开头和结尾。

url('^admin/$', views.RequestQueue, name="queue"),
url('^$', views.ps_request, name='requests'),

由于您使用的是Django 2.0,因此使用path()更容易:

path('admin/', views.RequestQueue, name="queue"),
path('', views.ps_request, name='requests'),

由于您使用的url()没有$,因此您对http://127.0.0.1:8000/requests/admin/10/的请求与url('admin', ...)网址格式匹配并处理。此url()没有id kwarg,因此视图使用默认的id=0

一旦你修复了它,最后一件事就是更改发布请求,以便将其发送到带有斜杠的URL。这可能需要更改模板。