开发者

Django urls conf

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 usu开发者_StackOverflow中文版al 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!!


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.


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})
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜