On 13 oct, 20:13, KillaBee <[EMAIL PROTECTED]> wrote: > I am want in to pass the user and a date, using the url. Like the > polls urls > (r'^(?P<poll_id>\d+)/$', 'intranet.timesheets.views.detail'), > > I tried these, but I am missing a piece: > urlpatterns = patterns('', > (r'^timesheet/(?P<username>)/$', > 'intranet.timesheets.views.times'), > (r'^timesheet/(?P<username>\d{20})/$', > 'intranet.timesheets.views.times'), > (r'^timesheet/(?P<user>\d{20})/(?P<date>\d{10})/$', > 'intranet.timesheets.views.times'), > (r'^timesheet/(?P<user>\d{20})/$', > 'intranet.timesheets.views.times'), > (r'^(?P<poll_id>\d+)/$', 'intranet.timesheets.views.login'), > ) > Is a understand it, P tells django that this is python code.
Nope. It tells the Python standardlib's re module that this is a named group. http://docs.python.org/library/re.html#regular-expression-syntax When it comes to Django's url/dispatch system, a named group is passed to your view function as keyword argument (while unnamed groups are passed as positional arguments). > d is the number of chars, Wrong again. '\d' means 'any digit'. > but how does it know what user or date mean? It doesn't. urls are strings, matching subpatterns are strings, and all that get passed to your view function - except for the mandatory 'request' argument - is passed as a string. I *strongly* suggest you: 1/ carefully (re-)read Django's FineManual 2/ learn regexps (documented in Python's FineManual, cf url above) Trying anything that comes to mind in the hope it will magically start working is a well known antipattern named "programming by accident" (aka "programming by permutation, cf http://en.wikipedia.org/wiki/Programming_by_permutation). Back to your actual question. Your specification is not clear, but I assume you want to build an url that looks like (parts that need to be captured and passed to the view between <>): http://your.domain.tld/timesheet/<user_id>/<date>/ the scheme and domain name not being of any interest here, we're left with: /timesheet/<user_id>/<date>/ Now to build the correct regexp, we need to know: 1/ what's a user_id 2/ what's a date For a first example, we'll define 1/ 'user_id' as a sequence of digits of arbitrary length, and 2/ 'date' as a "yyyymmdd" formated date. The (minimal) corresponding regexps are resp.: 1/ r"\d+" (any numeric character 1 or more times) 2/ r"\d{4}\d{2}\d{2}" (any numeric character 4 times followed by any numeric character 2 times followed by any numeric character 2 times). Note that this last one is far from perfect, but we'll come back on this later. At least it will match a valid date. So our url pattern, once 'assembled', might look like this: r"^timesheet/\d+/\d{4}\d{2}\d{2}/$" Now we do want the user_id and date part to be _captured_, so they are passed as arguments to our view function. The simplest way would be to just surround them with parens, ie: r"^timesheet/(\d+)/(\d{4}\d{2}\d{2})/$" But doing so, they would be passed as positional arguments (ie : the first matching group as first argument, the second as second argument). The drawback is that if your customer decides he prefers "/ timesheet/<date>/<user_id>/", then you not only have to modify your url pattern, but also your view function. Be sure that you'll forget the second modification and waste time debugging. So the solution is to use 'named groups', that Django will then pass as keyword arguments (cf http://docs.python.org/tutorial/controlflow.html#keyword-arguments). The regexp syntax for a named group is (?P<name>expression). In our case, this means: r"^timesheet/(?P<user_id>\d+)/(?P<date>\d{4}\d{2}\d{2})/$" One last point: the last part of the url pattern will indeed match a yyyymmdd date, but it will also match any arbitrary sequence of 8 numeric characters - in fact, we could as well write it "(?P<date> \d{8})" (you should by now understand what this means). This is no big deal since, for invalid dates, you probably won't have any corresponding record in the database. But anyway : once you'll have read the corresponding documentation and experimented a bit by yourself (hint : Python comes with an interactive interpreter which is quite handy for Q&D tests and API exploration - so learn to use it), you should be able to write a better pattern here. HTH --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---