So I'm building a fairly simple website that allows users to create and edit profiles. I'm in the process of creating the URLs for the site, which follow the following "rules":
www.website.com
should redirect to home.www.website.com/profile/person
should redirect toperson
's profile.www.website.com/profile/person/extra/useless/info
should redirect toperson
's profile, as the URL should be "trimmed" afterprofile/person/
.www.website.com/profile
should redirect back towww.website.com
which will redirect to home.
My code so far is as follows.
# my_site/urls.py
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^$', include('my_app.urls')),
url(r'^profile/', include('my_app.urls')),
url(r'^admin/', include(admin.site.urls)),
]
Part 2:
# my_app/urls.py
from django.conf.urls import url
from django.http import HttpResponse
from . import views
urlpatterns = [
url(r'^(?P<username>[\w-]*)/$', views.profile, name='profile'),
url(r'^(?P<username>[^\w-])/$', views.profile, name='profile'), # still link to the profile
url(r'^$', views.home, name="home"),
]
With this code, when the user enters www.mysite.com/profile
, the user is redirected to the home page, but the address bar still reads www.mysite.com/profile
, which I do not want. I want it to read www.mysite.com
. Also, the 3rd rule in the rule list I gave above is not obeyed. I was thinking of having a URL "cleaning" function, which trims unwanted parts of the URL, but I have no idea how to do this. Any help would be greatly appreciated.
Thank you very much.