tags:

views:

71

answers:

2

So I worked with Django for a bit and understand regex rudimentary.

I know if there is request it "maps"(not sure what that means) the urls to a certain definition in the view.

That is clear and understandable for one page. But what if I want to design a urlpattern for multiple pages and/or the whole website. I dont understand that part.

Is there a way to do that without regex? If not: What is way to create robust structure with regex? Where I can than add and remove pages quickly.

How do flatpages differ form other pages in this regard?

If that is possible and reasonable I would like to achieve the following with my urls.py?

  1. Flatpages for the usual sites a website needs:

Home About Media . . . Contacts

  1. Dynamically created sites within that that are similar to Webgallery (But are not)

Where on the first site I have Text and some checkboxes. And then have one Item of media on every page with a next button.

Urls could look like this

myapp/start/

and then

myapp/start/1 to n

And myapp/ is in the main navigation and a flatpage.

I am used to do static pages and I somehow dont understand how I can get a structure into these different pages. It seems flatpages are static. So I can work easier with them. But maybe later I will run into problems using this approach.

If there would be any great way to quickly understand regexes or how to create a site structure in Django: Please tell me.

Thanks!!

+1  A: 

in urls.py, you are not really mapping a URL to a page, you are mapping a URL to a function which can render a page. you can map multiple URLs to the same function, and you can have the function return different things based on the URL, if you wanted.

so, for your case, you might have something that looks like

(r'^myapp/$', 'myapp.show_main_navigtaion_page'), # if the url is "myapp/" only, show main nav
(r'^articles/start/$', 'myapp.show_start_page'),
(r'^articles/start/(\d+)/$', 'myapp.do_something_with_start'),

some basic regex stuff in here: "^" character means start of url, "$" means end of string, the "()" characters capture whatever is inside them, "\d+" matches one or more numbers.

Paul Sanwald
Thank you very much! And how are the article/start/page1 to page 40, create? One by one? Or what can I use to create that in a automatic way? Paginator? Thats what I dont understand.
MacPython
articles/start/1 and articles/start/40 are both matched by the 3rd regex with the \d in it, and are valid URLs with the above in urls.py. is that what you're asking?
Paul Sanwald
Thanks I tried the 3rd regex example.Made a simple Hello world HttpResponse in views and it did not work.
MacPython
A: 

You should split your urls in apps:

urlpatterns = patterns('',
    (r'^accounts/', include('my.accounts.urls')),
    (r'^gallery/', include('my.gallery.urls')),

And then you can create structures using regex:

urlpatterns = patterns('my.gallery.views',
    (r'^photos/new/$', 'photo_new'),
    (r'^photos/$', 'photo_list'),
    (r'^photos/by_user/(?P<user_id>\d+)/$', 'photo_list'), # capture (\d+) into user_id variable
    (r'^photos/by_category/(?P<cat_id>\d+)/$', 'photo_list'),
)

I use photo_list three times, because it accepts optional arguments (user_id and cat_id). It looks like that:

def photo_list(request, cat_id=None, user_id=None):
if cat_id is not None:
    c = get_object_or_404(Category, pk=cat_id)
    q = c.photo_set.all()
elif user_id is not None:
    u = get_object_or_404(User, pk=user_id)
    q = u.photo_set.all()
else:
    q = Photo.objects.all()
return render_response(request, "gallery/photo_list.html",{
    "photos":q})
joozek
Thanks a lot ! ad First Block of code:What does include do?ad second Block:(?P<user_id>\d+) can map urls like hans/1, hans/2 etc but not hans/1a ?ad third block:get_object_or_404 is a django build in function?Thanks again
MacPython
ad 1) It includes urls files in subdirectories of your project. An example of one is File no. 2ad 2) Yes. For more read http://docs.python.org/howto/regex.html3) Yes, it's defined in django.shortcuts
joozek