0

If users' url type patterns are settled, for example,

1. myurl.com/feeds/recnets/

2. myurl.com/feeds/users/

3. myurl.com/feeds/tags/

4. myurl.com/feeds/~

I want to make a dictionary to pass specific function from those patterns.

So I make a dictionary type in urls.py and passing its dictionary parameters. (see below)

 1 import os.path
 2 from django.conf.urls import patterns, include, url
 3 from bookmarks.views import *
 4 from django.contrib import admin
 5 from django.views.generic import TemplateView
 6 from bookmarks.feeds import *
 7 
 8 admin.autodiscover()
 9 
 10 site_media = os.path.join(
 11         os.path.dirname(__file__), 'site_media'
 12 )
 13 
 14 feeds = {
 15     'recents' : RecentBookmarks(),
 16     'user' : UserBookmarks()
 17 }
 18 
 19 urlpatterns = patterns('',
 20     # Feeds 
 21     (r'^feeds/(?P<url>.*)$',
 22         feeds),

As I expected, it did not work because urls.py did not differentiate parameters' name.

I also referred django project document to resolve this problem, but I could not found it how to pass hash arguments to function at url side.

3
  • to make clear . u have different functions in view for recent,user,tag.is it right Commented May 2, 2014 at 9:21
  • @SundarNataraj yes it is. I made it and imported it in my project folder Commented May 2, 2014 at 9:23
  • Have the below code looks as u intented. Commented May 2, 2014 at 9:33

2 Answers 2

1

To further elaborate the Above answer.

It is always better to Keep App specific urls in each app folder's urls.py file. You can then include urls of each app in your root urls.py file.

e.g

Project/
  -- urls.py   # Root urls.py
  -- app1/
     --- urls.py   # App specific urls.py
  -- app2/
     --- urls.py

And each URL should have its own Pattern and related to a view e.g

### app1/urls.py ###    
urlpatterns = (
        url(r'^recents/$', RecentBookmarks.as_view()),
       # Samefor all the urls
    )

For more info on including url patterns see here: https://docs.djangoproject.com/en/1.6/topics/http/urls/#including-other-urlconfs

For further learning, you can also look into how to use url Name-spacing here: https://docs.djangoproject.com/en/1.6/intro/tutorial03/#namespacing-url-names

Sign up to request clarification or add additional context in comments.

Comments

1

in project folder: according Django 1.6 urls.py

from django.conf.urls import include, url
from bookmarks import *

urlpatterns = [
    # ... snip ...

    url(r'^feeds/', include('bookmarks.urls')),
    # ... snip ...
]

in bookmarks folder create file urls.py

from bookmarks import *

from django.conf.urls import url

urlpatterns = [
    url(r'^recents/$', 'bookmarks.view.RecentBookmarks'),
    url(r'^user/$', 'bookmarks.view.UserBookmarks'),
   #so on for all the urls
]

Note: feel free edit the code if there is mistake in importing

2 Comments

Do you mean dissociate urls into two .py files, first, urls.py in project folder, and bookmarks/urls.py to hash all of user's input?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.