servvs
servvs

Reputation: 74

django urls that aren't defined

I am having an issue where urls that don't "exist" are able to display. For instance, I go to a url at /myapp/login and it works. I then go to /myapp/login/login and it still works. I then go to /myapp/insertrandomcharactershere/login and it still works. This will work for every url I have in my subpath of /myapp/ so /myapp/register, /myapp/login and /myapp/myaccount. It seems that as long as it ends in a name in my urlconf it will work. I have been unable to fix this issue so I am adding some code from my login page and my urlconfs that should hopefully contain the problem.

I should 404 on anything that isn't /myapp/, /myapp/login/, /myapp/register/, /myapp/myaccount/

http://paste.pound-python.org/show/31613/

#django/urls.py
urlpatterns = patterns('',
    url(r'myapp/', include('myapp.urls')),
    url(r'^admin/', include(admin.site.urls)),
)

#myapp/urls.py
urlpatterns = patterns('myapp.views',
    url(r'^$', 'index', name='index'),
    url(r'register/$', 'register', name='register'),
    url(r'myaccount/$', 'myaccount', name='myaccount'),
    url(r'login/$', 'login_page', name='login'),
    url(r'logout/$', 'logout_user', name='logout')
)

#myapp/login.py
def login_page(request):
    if request.user.is_authenticated():
        return HttpResponseRedirect(reverse('index'))
    form = LoginForm(request.POST or None)
    if form.is_valid():
        username = form.cleaned_data['username']
        username = username.capitalize()
        password = form.cleaned_data['password']
        try:
            User.objects.get(username=username)
        except:
            return render(request, 'myapp/login.html', {
                'form': form,
                'error': "Account doesn't exist"
                })
        user = authenticate(username=username, password=password)
        if user is not None:
            if user.is_active:
                login(request, user)
                return HttpResponseRedirect(reverse('index'))
            else:
                return render(request, 'myapp/login.html', {
                    'form': form,
                    'error': 'Account is disabled or banned',
                    })
        else:
            return render(request, 'myapp/login.html', {
                'form': form,
                'form': 'Authentication Error!'
                })
    return render(request, 'myapp/login.html', {
        'form': form,
        })

{% if not user.is_authenticated %}

#myapp/templates/myapp/login.html
<strong>Login</strong>
<form action='{% url 'login' %}' method="post">{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Login">
</form>
{{ error }}
{% endif %}

Upvotes: 0

Views: 139

Answers (3)

kasun
kasun

Reputation: 191

You need to mark start of patterns with a '^'.

Ex:

urlpatterns = patterns('',
    url(r'^myapp/', include('myapp.urls')),
    url(r'^admin/', include(admin.site.urls)),
)

#myapp/urls.py
urlpatterns = patterns('myapp.views',
    url(r'^$', 'index', name='index'),
    url(r'^register/$', 'register', name='register'),
    url(r'^myaccount/$', 'myaccount', name='myaccount'),
    url(r'^login/$', 'login_page', name='login'),
    url(r'^logout/$', 'logout_user', name='logout')
)

Upvotes: 0

n3rV3
n3rV3

Reputation: 1106

I think the urls have a ^ missing:

Following line:

url(r'login/$', 'login_page', name='login'),

should be:

url(r'^login/$', 'login_page', name='login'),

Upvotes: 5

Rohan
Rohan

Reputation: 53336

You need to specify in your url that it should start with string that you want using '^' at the start.

So you can update your urls.py line

url(r'myapp/', include('myapp.urls')),

to

url(r'^myapp/', include('myapp.urls')),

You may want to do this for other urls in myapp.urls.

Upvotes: 0

Related Questions