0

What does not work: If i create a link (e.g. on domain.tld/main) with href="#" i get the link displayed as domain.tld/main# and not as i wanted it to be as domain.tld/main/#. I want the consistency as it is displayed on my index page with no url subdirectory as domain.tld/#.

Configuration:

urls.py

urlpatterns = [

url(r'^$', views.UserLogin),

url(r'^logout$', 'django.contrib.auth.views.logout', {'next_page':'/'}),

url(r'^main$', views.Main),

]

views.py

def Main(request):
    if not request.user.is_authenticated():
         return HttpResponseRedirect("http://www.domain.tld")
    else:
         return render(request, "main")
rwx
  • 696
  • 8
  • 25
  • What do you need the link to include the slash for? what problem are you trying to fix that you need this fix for? – Sayse Feb 15 '16 at 21:38
  • @Sayse Only for consistency, because on the index page `domain.tld` it's displayed as `domain.tld/#` – rwx Feb 15 '16 at 21:43

2 Answers2

5

The only way you're really going to be able to have your intended slash is if you include it in the url all the time

url(r'^main/$', views.Main),

So thats a decision you'll have to make as to whether or not thats acceptable. #'s main purpose is only to scroll to the top of the page

You can read more about that in this question

Community
  • 1
  • 1
Sayse
  • 42,633
  • 14
  • 77
  • 146
  • 3
    Note that most Django sites *do* always include the trailing slash in urls. – Daniel Roseman Feb 15 '16 at 21:52
  • 1
    @DanielRoseman Is it best practice to have always a trailing slash in Django (and redirect them) or should i do it the "old" way to differentiate between directories and files? – rwx Feb 15 '16 at 22:03
  • 1
    @rwx - [When should I use a trailing slash in my URL?](http://stackoverflow.com/q/5948659/1324033) – Sayse Feb 15 '16 at 22:14
1

The way it works now is a default way. '#' sign is reserved in urls as the delimiter of a fragment identifier, so it does not point at a directory of a site and it is simply added to the current path.

The reason why it works as you expect in case of index page is the impossibility of changing your domain name - '#' cannot be directly added to www.domain.tld but only through a slash sign.

chem1st
  • 1,624
  • 1
  • 16
  • 22