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'),
]
Community
  • 1
  • 1
Always_a_learner
  • 4,585
  • 13
  • 63
  • 112

2 Answers2

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 %}">
levi
  • 22,001
  • 7
  • 73
  • 74
  • Thanks @levi. Let me implement your solution to check if it works otherwise I will post my urls.py in question. – Always_a_learner Aug 11 '16 at 06:17
  • this gave me an error:Reverse for 'myprfile' with arguments '()' and keyword arguments '{'username': 'simer'}' not found. 0 pattern(s) tried: [] – Always_a_learner Aug 11 '16 at 06:44
  • 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: [] – Always_a_learner Aug 11 '16 at 06:46
  • @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` – levi Aug 11 '16 at 06:49
  • I have profile and myprofile both but none of them match. Please see my updated question. Thanks for your help @levi – Always_a_learner Aug 11 '16 at 06:56
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/120661/discussion-between-levi-and-simer). – levi Aug 11 '16 at 06:59
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.

Sayse
  • 42,633
  • 14
  • 77
  • 146