开发者

django problem:captured parameter in parent urlconf , incude() and reverse match

开发者 https://www.devze.com 2023-02-25 00:52 出处:网络
If I want to have django app called app1 which can make a user post note to someone\'s page and to his/her own page.

If I want to have django app called app1 which can make a user post note to someone's page and to his/her own page. And if the user is logged in ,no need using username in the url, but the username parameter in url can be acts as parent in urlconf.

To make it clear:

urls.py

urlpatterns = patterns(''

    #if registered user/anonymous user visit someone's page
    url(r'^/foo/users/(?P<username>\w+)/app1/',include('app1.urls', namespace='myapp1')),

    #if user is logged in in his own page 
    url(r'^app1/', include('app1.urls', namespace='myapp1')), 
    ...
)

app1/urls.py

urlpatterns = patterns('',
    # I expect this pattern receives the username parameter from above 
    url(r'^note/add/$', app1_views.add_note,
     name='add_note'),

    url(r'^note/add/$', app1_views.add_note,
    { 'username':None}, name='add_note_own'),
    ...
    ...    
)    

app1/views.py

def add_note(request, username=None):
    ...
    ...

First question:

Now for example john is logged in and on jack's notes page john want to post a note.

I want to b开发者_如何学运维e able to do something like this or near something like this:

template app1/notes.html

{% if request.user.is_authenticated %}
    {%if in his/her own note page %}
        <a href="{% url add_note_own %}">add note</a> Expected generated url: www.domain.com/app1/add
    {%else}
        <a href="{ %url add_note %}">add note</a> Expected generated url: www.domain.com/foo/jack/app1/add
    {%endif%}
{% endif %}

Is this possible?

Another thing,

if john wrote note in jack's page, and django gives the note id == 3,

so to show that note, only these urls are valid:

www.example.com/foo/jack/app1/3

www.example.com/foo/app1/3 (if jack logged in)

Second question:

what I want to achieve is reverse match can accept captured parameter up to to the parent urlconf when include() is involved in url configuration. Can this be done?

Or if you get what I mean and can provide simpler solution , please do so :)

sorry if this post is confusing, I am confused myself. Thanks alot for the patience.

I'm using django 1.2.5


you need to pass an username, one example can be:

urlpatterns = patterns('',
    # I expect this pattern receives the username parameter from above 
    url(r'^note/add/(?P<username>[^/]+)/$', app1_views.add_note, name='add_note'),

    # username is an optional argument, so no need to pass it
    url(r'^note/add/$', app1_views.add_note, name='add_note_own'),
)  

and then in template:

{% if request.user.is_authenticated %}
    {% if page.owner == request.user %}
      <a href="{% url add_note_own %}">add note</a>
    {% else %}
       <a href="{% url add_note request.user.username %}">add note to {{ request.user.username }}</a>
    {% endif %}
{% endif %}
0

精彩评论

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