Thanks. I had heard about 'slugs' but haven't gotten far enough to find out what they were yet. I managed to do a simplified version of what you're explaining by matching any alphanumeric character within the 'jobs' URL section. I did add a second argument to the view and am able to get things working now.
Although I've been working with Python quite a bit, none of that has been web development, but rather back-end systems administration type work. I've always used PHP (and prior to that, Perl) for web development so Django, or MVC type frameworks, in general are making my head spin. It is definitely starting to click though. I just need to keep reading the tutorial as I work. On Sep 17, 11:39 am, Daniel Roseman <dan...@roseman.org.uk> wrote: > On Sep 17, 4:10 pm, Thomas <thomas.e.rectenw...@gmail.com> wrote: > > > > > Hello, > > > I'm learning Django and have some questions regarding passing data to > > urlpatterns. Right now, I have something like this in my urls.py: > > > urlpatterns = patterns('', > > (r'^$', index), > > (r'^list_jobs/', list_jobs), > > ) > > > This works fine. I.e., any URL that matches ^list_jobs/ is handled by > > the list_jobs method in my views.py. That calls a template which > > displays the list of jobs in an HTML table. > > > So, now I have a list of jobs displayed in HTML, i.e. something like: > > > Job | Severity | Message > > > morning_job | INFO | Testing 123 > > evening_job | ERROR | Testing 456 > > > My next step, I figured would be to have a user click on 'job1' or > > 'job2' in the table and call up information on that job in a new > > window. So, I imagine that the correct way to do this within Django > > would be to have the HREF point to a the job in the URL. I.e.: > > >http://myserver:8000/morning_jobhttp://myserver:8000/evening_job > > > My next though was... grr, how do I do this? So, I figured I can get > > the job names in the urls.py by first creating a method: > > > from myapp.models import * > > def get_job_names(): > > job_names = [] > > for j in TransJob.objects.all(): > > job_names.append(j.job) > > return job_names > > job_names = get_job_names() > > > So, now I have a list (job_names) that would contain: > > > ['morning_job', 'evening_job'] > > > I think I'm getting close. Final step is to somehow take that list > > and put it within urlpatterns. So, I tried something like: > > > urlpatterns = patterns('', > > (r'^$', index), > > (r'^list_jobs/', list_jobs), > > (job_names, list_job_details), > > ) > > > Here is where I get lost. I think I'm going down the wrong path > > here. The above comes back with a TypeError saying "unhashable type: > > 'list'" ... I'm guessing that you can't put a list in place of the raw > > string that the patterns method expects. So, my question is... how do > > I get patterns to match all results for the list. > > > I hope this makes some sense. I tried to be verbose as I'm new to the > > framework. Thanks for any assistance. > > > Regards, > > Thomas > > Yes, you've got yourself a bit turned about. > > The way to think about it is that each job in your list has its own > page, where it shows its full details. So you need a view that takes a > parameter - the job name or id - and displays a page for that job. > This is done by using the regex match to extract the job_name part of > the URL: > > (r'$/job/(?P<slug>[-\w]+)/$', 'job_detail'), > > So this passes a 'slug' parameter to the 'job_detail' view. (A slug is > a newspaper term for a short label with just alphanumeric characters, > letters or hypens, which is ideal for a URL - I recommend having a > slug field in your TransJob model, alongside the name which might not > itself be suitable for URL.) > > Now your view starts like this: > > def job_detail(request, slug): > job = TransJob.objects.get(slug=slug) > > ... and you can pass that job to the template, or whatever. (In fact > you might want to use the get_object_or_404 method here, to return a > proper error if there's no job with that slug, but I'm leaving it > simple for now.) > > Hope this makes sense. You'll need some way of getting the URL for > each job in your original list view - I recommend defining a > get_absolute_url method on the TransJob model. > -- > DR. --~--~---------~--~----~------------~-------~--~----~ 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 django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---