2

I have to page in my django powered website. index.html and books.html

I have this link to the books.html in my first page

<a href="books.html">books</a>

and this one is in books.html for getting back to the first page.

<a href="index.html">Home</a>

and my url conf is like this

urlpatterns = patterns('',
    url(r'^$', 'mysite.views.home', name='home'),
    url(r'^books.html/$', 'mysite.views.bookpage', name='books'),
    url(r'index.html/$', 'mysite.views.home', name='home'),
)

and views.py

def home(request):
        return render_to_response('index.html')

def bookpage(request):
        return render_to_response('books.html')

When I click on the second link to get back home. my url address looks like this

mysite.com/books.html/index.html/

How can I change url back to mysite.com/ when clicking on the second link in books.html?

sorry if the title is not appropriate. i didn't know what would be my question title

Update

I've deleted url(r'^index.html/$', 'mysite.views.home', name='home') from my url patterns and used <a href="{% url 'home' %}"> in the second page. But nothing happens when I click on that link

Ghasem
  • 14,455
  • 21
  • 138
  • 171

3 Answers3

3
  1. With Django don't write <a href="anything.html">. You should use url template tag for page addresses. More on url template tag here: https://docs.djangoproject.com/en/1.7/ref/templates/builtins/#url

Example: <a href="{% url 'home' %}">

  1. In your urls.py you have ^books.html/$ and index.html/$. Notice the missing ^ before index? You may want to add it.

  2. Don't use same name for different patterns in urls.py.

Maciek
  • 3,174
  • 1
  • 22
  • 26
3

Two issues that have been missed in the other answers:

  1. You're missing a ^ in your third URL: url(r'^index.html/$', 'azahra.views.home', name='home'),
  2. You have two URLs with the same name='home'. One of them should be changed.

As mentioned, you should also be using {% url 'url_name' %} template tags, instead of hard-coding in your templates.

Also, unless your application is actually named mysite, you need to change that to reference your actual views...

rnevius
  • 26,578
  • 10
  • 58
  • 86
2

You're missing a ^:

urlpatterns = patterns('',
    url(r'^$', 'mysite.views.home', name='home'),
    url(r'^books.html/$', 'azahra.views.bookpage', name='books'),
    url(r'^index.html/$', 'azahra.views.home', name='home'),
)

Take a look at the url template tag - it'll simplify your URL routing in your templates and move you away from these types of errors.

Celeo
  • 5,583
  • 8
  • 39
  • 41