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

5
  • Is the view referenced in the sample mysite.views? or azahra.views? Commented Mar 16, 2015 at 17:10
  • @Brandon Sorry. its mysite. my bad. Commented Mar 16, 2015 at 17:11
  • Help me understand why you want/need index.html to point to the home view instead of / Commented Mar 16, 2015 at 17:12
  • @Brandon because / doesn't work when I use it in href Commented Mar 16, 2015 at 17:15
  • I think there's something larger at fault if / isn't working to get you back to the site root. That should work no problem. Commented Mar 16, 2015 at 17:16

3 Answers 3

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.

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

4 Comments

But nothing happens when I use <a href="{% url 'home' %}">
That's most likely because there are two patterns named "home"
But it is in my urlconf. isn't it?
The problem is somewhere in my js codes. it works when I try it in a test <a> tag. thank you
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...

5 Comments

I've deleted url(r'^index.html/$', 'azahra.views.home', name='home'), from my url patterns and used <a href="{% url 'home' %}"> in the second page. But nothin happens
What do you mean "nothing happens"? Do you get an error?
No. there is no errors. its like you don't click at all!
Do you actually have an app called mysite?
The problem is somewhere in my js codes. it works when I try it in a test <a> tag. thank you
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.

1 Comment

That throws` Page not found (404)`

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.