1

I am working on django project(which is at verge of completion) and I have structure like something below: project name: Mysite App name: myapp

project urls : 127.0.0.1/myapp/blog etc

my requirement is to exclude my app from website (through out) and adding usernames in profile page like:

  • 127.0.0.1/blog
  • 127.0.0.1/products
  • 127.0.0.1/search

and for profile page: 127.0.0.1/simer123/myprofile

I have took from various SO questions and able to exclude the "myapp" part from my urls. and Also able to include "username" in url for specific page. SO question1 SO question2 these questions really helped and I found a way out to include user name in my url.

But now again I am stuck because throughout the project I have used things like:

 return HttpResponseRedirect(reverse_lazy('myapp:myprofile'))

And similarly in template part like:

<a href="{% url 'myapp:myprofile' %}">

How can I manage to convert them?

Can Anyone explain this with some example.

Thanks

Update: urls.py file in mysite folder looks like below:

urlpatterns = [
    url(r'^autocomplete/', include('autocomplete_light.urls')),
    url(r'^$', 'myapp.views.index'),
    url(r'^', include('myapp.urls', namespace="myapp")),
    url(r'^myapp/', include('myapp.urls', namespace="myapp")),
    url(r'^admin/login', adminLogin),
    #url(r'^static/(?P<path>.*)$', views.serve),
    url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {
        'document_root': settings.MEDIA_ROOT,
    }),
    url(r'^(?P<username>\w+)/', include('myapp.urls', namespace="myapp")),
    url(r'^(?P<username>\w+)/myapp/', include('myapp.urls', namespace="myapp")),
]

urls.py in myapp folder includes urls like below:

urlpatterns = [
    # ex: /myapp/
    url(r'^autocomplete/', include('autocomplete_light.urls')),
    url(r'^$', views.index, name='index'),
    url(r'^about/$', views.about, name='user_about'),
    url(r'^login/$', views.login, name='login'),
    url(r'^newaccount/(?P<uid>.*)/$', views.newaccount, name='newaccount'),
    url(r'^logout/$', views.logout, name='logout'),
    url(r'^myprofile/$', views.profile, name='profile'),
]
3
  • can you post your urls.py Commented Aug 11, 2016 at 6:08
  • 1
    @levi I have updated my question. Commented Aug 11, 2016 at 6:32
  • I updated my answer. Commented Aug 11, 2016 at 6:32

2 Answers 2

1

Your base url doesn't need to mention myapp at all, just set your url that includes myapp to r'^'. You can still namespace the urls and everything the same as before.

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

Comments

1

You can pass a kwargs dict to your reverse and reverse_lazy function in order to mapping it with your url pattern.

reverse_lazy('myapp:profile', kwargs={'username': username})

And in your template, just pass it as positional argument

<a href="{% url 'myapp:profile' username %}">

6 Comments

Thanks @levi. Let me implement your solution to check if it works otherwise I will post my urls.py in question.
this gave me an error:Reverse for 'myprfile' with arguments '()' and keyword arguments '{'username': 'simer'}' not found. 0 pattern(s) tried: []
I have this url included in my myapp urls.py : url(r'^myprofile/$', views.myprofile, name='myprofile'), and I have def in views,py as def myprofile(request, username). I got error as : Reverse for 'myprofile' with arguments '()' and keyword arguments '{'username': 'simer'}' not found. 0 pattern(s) tried: []
@Simer Fixed, you need to change myprofile for profile, cuz your url pattern anme is profile not myprofile. So, this is the correct pattern myapp:profile
I have profile and myprofile both but none of them match. Please see my updated question. Thanks for your help @levi
|

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.