0

I am trying to configure urls.py to pass information through the URL.

My main urls.py file includes the following requests/urls.py when the url matches "requests/"

full requests/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'),
]

from 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)

When I run the development server with url like:

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

the value of "id" still equals the default value.. 0

Shouldn't it update to the the value of the number in the URL?

EDIT - Traceback: Environment:

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.
mcm66103
  • 15
  • 5
  • 3
    Your url pattern does not contain `requests/` - if you have and `include()` then please show that as well. – Alasdair Mar 30 '18 at 18:18
  • And now that I look more closely you have mixed up `path()` and `url()`. If you are using Django 2.0, you want `path('admin//', views.RequestQueue),` – Alasdair Mar 30 '18 at 18:29
  • I made the changes mentioned in the other thread, but it still looks like I am encountering a problem with this specific section of code. I see the difference between url() and path(), but the value is still not passed to my variable. – mcm66103 Mar 30 '18 at 18:46
  • How are you checking the value of `id`? In the view you have posted you are only using it in the `POST` part. Perhaps there's a problem somewhere else in your URLs. Note that your URL is for `http://127.0.0.1:8000/requests/admin/10/` *with* a trailing slash. – Alasdair Mar 30 '18 at 18:52
  • I am new to Python, and I don't understand the importance of the trailing slash. The Traceback tells me the following: request_update = Request.objects.get(pk=id) local vars -> id -> 0 – mcm66103 Mar 30 '18 at 19:08
  • Please switch the traceback to the copy and paste view, then [edit] your question to include it - it will be much more readable. Since you have a trailing slash in `path('admin//', ...)`, that means it will only handle requests to `http://127.0.0.1:8000/requests/admin/10/`. That means that your request to `http://127.0.0.1:8000/requests/admin/10` is being handled by a different URL pattern (and possibly a different view). I can't tell any more than that because you haven't included the rest of your URLs. – Alasdair Mar 30 '18 at 19:10
  • I added the full urls.py file and included the traceback! – mcm66103 Mar 30 '18 at 19:20

1 Answers1

0

Your first URL pattern is:

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

This means it will only handle requests to admin/<id>/. The trailing slash is important - it will not match admin/<id>.

The next issue is that you using url() incorrectly. When you use url(), it uses regexes, so you need ^ and $ to mark the beginning and end of the string.

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

Since you're using Django 2.0, it's easier to use path():

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

Because you are using url() without $, your request to http://127.0.0.1:8000/requests/admin/10/ is matched and handled by the url('admin', ...) URL pattern. This url() does not have an id kwarg, so the view uses the default id=0.

Once you've fixed that, the last thing is to change the post request so that it is sent to the URL with the trailing slash. This probably requires a change to your template.

Alasdair
  • 298,606
  • 55
  • 578
  • 516
  • Thank you so much. I have been watching videos and following tutorials, but I couldn't quite get it. Thanks for the help!! – mcm66103 Apr 01 '18 at 14:01