Ankit Kulshrestha
Ankit Kulshrestha

Reputation: 111

internal url redirection in django

I'm fairly new to Django(experienced in Python though). I'm trying to make a new website using it for the first time and this problem is bothering me.

Here's the user flow:

  1. The user goes to my site www.newco.com
  2. He is greeted by the homepage and links for 'login','signup' ,'about' etc.

  3. The user clicks on those links and goes to the respective pages.

Here's my root urlconf:

from django.conf.urls import include, url
from django.contrib import admin

urlpatterns = [
    url(r'^$',include('homepage.urls')),
    url(r'^admin/', include(admin.site.urls)),
    url(r'^login/$', include('userlogin.urls')),
    url(r'^signup/$', include('userlogin.urls'))

]

Here's my homepage.urls:

from django.conf.urls import url
from . import views

urlpatterns=[
  url(r'^$',views.index,name='index'),
  url(r'^about$',views.about,name='about'),

]

and finally here's my homepage.views file:

from django.shortcuts import render

def index(request):

    return render(
        request,
        'index.html'

    )



def about(request):
    return render(
        request,
        'about.html'


     )

When I go to my development server's homepage, the homepage renders correctly. I tried two approaches for going to the 'about us' page:

  1. I hardcoded the URL as <a href="/about"> About us </a>. That redirected me to the homepage.

  2. I coded the URL as {% url "about" %}. This gives me the error: Reverse for 'about' with arguments '()' and keyword arguments '{}' not found. 1 pattern(s) tried: ['$about$'].

How should I proceed about this problem, since this is a specific instance of a larger problem. Thanks for reading.

Upvotes: 1

Views: 1528

Answers (1)

Gocht
Gocht

Reputation: 10256

You get this error because you have defined like this:

url(r'^$',include('homepage.urls')),

and it should be:

url(r'^',include('homepage.urls')),

When you use $ you are telling that the regex ends there. That should fix your problem.

Upvotes: 2

Related Questions