But if I use the pattern:

*urlpatterns = [ *
*    url(r'^blank/.*$', views.BlankMore, name='blankMore'),*
*]*
I can enter:

     localhost:8000/uA/blank/

and the pattern will match, but if I enter:

      localhost:8000/uA/blank/abc

I get an error message: "Page not found (404)"

I could be doing something wrong here, however I am more inclined to
believe this is a bug. My error? Bug?


This appears to be working as designed. Your interpretation of the regex
algorithm behavior is incorrect. In most cases, the matching algorithm will
take the first and almost always shortest match (there are probably some
exceptions nestled deep in the Python re module).

The .* modifier means "match any character (.) zero or more times (*)".
Since blank/ matches the .* zero times, it is a match for your expression.

What you are likely looking for is something like r'^blank/[\w-]+$' which
would capture any characters a-z, A-Z, 0-9, and a hyphen (-). This is
typically used to capture slugs, so you may not need the hyphen.

Also bear in mind that the Django setting for APPEND_SLASH is True, which
can interfere with your regular expression matching when Django
automatically redirects and adds a / at the end of the URL by default if
nothing matches. If your regexes are broad enough, it shouldn't be an
issue, though.

https://docs.djangoproject.com/en/1.11/ref/settings/#append-slash





Problem 2 - Similar to problem 1 except I would like to capture a value in
the URL

If I use the pattern:


*urlpatterns = [ *
*    url(r'^pass/val(?P<val>.*)/$', views.PassVal, name='passval'),*
*]*

and I enter the url:

     http://localhost:8000/uA/pass/val12/

the browser displays the value for 'val' as 12.

But if I use a url pattern like this:

*urlpatterns = [ *
*    url(r'^pass/(?P<val>.*)/$', views.PassVal, name='passval'),*
*]*

and I enter the url:

     http://localhost:8000/uA/pass/val12/

the browser displays the value 'val12', as expected.

but if I enter the url:

     http://localhost:8000/uA/pass/?abc=12/

I get an error message: "Page not found (404)"

What I would like to happen in this case is that the value "?abc=12" be
passed to the view. I know the '?' is a special character here but not to
the browser or Django.  My expectation is that the browser would pass it as
part of the request and that Django would do likewise. Is this a bug? My
error?


Here I believe Django is getting in the way, but for the right reasons.
You're attempting to capture GET arguments to the URI request. The URL
dispatcher in Django is not designed to capture these arguments. Instead,
I'm guessing the request parser for Django first strips the GET arguments
(the ?abc= portion), and passes the remaining portion of the URL to the URL
dispatch process where your regexes can be processed. You should be
retrieving your GET arguments via request.GET in a function-based view or
self.request.GET in a class-based view.


https://docs.djangoproject.com/en/1.11/intro/tutorial01/#url-argument-regex
- Explains how GET/POST values are swallowed by the request processor
before the URL is sent to the dispatcher.
https://docs.djangoproject.com/en/1.11/intro/tutorial04/#write-a-simple-form
- Example of using request.POST (which is equivalent to request.GET, just
depends on the verb in use)
https://docs.djangoproject.com/en/1.11/ref/request-response/#django.http.HttpRequest.GET
- Details how request.GET works as a QueryDict


The broader question really revolves around what you are trying to
accomplish? You should use either REST-style URL keywords that are captured
by the URL dispatcher, or use the request.GET/POST request to gather
GET/POST arguments. In general, I've seen the majority of developers
default to using URL matching to control what objects are being looked at,
and use GET/POST arguments for minor tweaks such as filtering, meaning that
a majority of the time, no ?arguments are used within Django. It's
perfectly acceptable to go that route though, and Django fully supports it.

-James

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CA%2Be%2BciX-vgzjTU74yzypkXQpfQJXYpEuSzYivgDXJ6Uq9UZLDw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to