Unable to match URL in URL.conf in Django? - python

What am I doing wrong ?
My URL : http://localhost:8000/login/
The DEBUG log from Django :
Page not found (404)
Request Method: GET
Request URL: http://localhost:8000/login/
Using the URLconf defined in dealers.urls, Django tried these URL patterns, in this order:
^login|home/ ^login/$
^login|home/ ^home/$
^login|home/ ^home/dealer/(?P<dealer_id>\d+)/$
^admin/
The current URL, login/, didn't match any of these.
urls.py file :
from django.conf.urls.defaults import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
(r'^login|home/',include('dealerpanel.urls')),
(r'^admin/', include(admin.site.urls))
)
dealerpanel/urls.py :
from django.conf.urls.defaults import patterns, include, url
urlpatterns = patterns('dealerpanel.views',
(r'^login/$','login'),
(r'^home/$','home'),
(r'^home/dealer/(?P<dealer_id>\d+)/$','details')
)

## urls.py
urlpatterns = patterns('',
(r'',include('dealerpanel.urls')),
(r'^admin/', include(admin.site.urls))
)
Change the prefix for including dealerpanel.urls to be the empty string. I think the way you have it structured it would actually be looking for a url like:
/login/login/
/login/home/
/home/login
...

Add something like this to your dealerpanel/urls.py so that http://localhost:8000/login/ will hit the view dealerpanel.views.target_view:
urlpatterns = patterns('dealerpanel.views',
...
(r'','target_view'),
)
This means the empty string after login (or home) will match the target_view

Related

Django new URL not working

I'm trying to access a URL, http://localhost:8000/calc, but it gives me this error:
Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
^admin/
^$ [name='home']
^calc/
The current path, calc, didn't match any of these.
This is what I currently have for mysite URL and secondapp URL:
# mysite/urls.py
from django.conf.urls import url, include
from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'',include('firstapp.urls')),
url(r'^calc/',include('secondapp.urls')),
]
# secondapp/urls.py
from django.conf.urls import url
from secondapp.views import CalcPage
urlpatterns = [
url(r'calc/', CalcPage.as_view(), name='calc'),
]
As per the URL configuration you have stated in mysite.url and secondapp.url you link would be
localhost:8000/calc/calc/
which IMO would be confusing, if I am correct, URL you want is
localhost:8000/calc/
For that you have to change the url you have defined in secondapp.url to
from django.conf.urls import url
from secondapp.views import CalcPage
urlpatterns = [
url(r'$', CalcPage.as_view(), name='calc'),
]
This would make the ClacPage accessible on
localhost:8000/calc/
See / is very important when defining URLs.
use below reg-ex in urls.py
r'^calc/$'

Django - page not found 404

I'm trying to create a site using Django. I have the index view working, however I want to create a simple custom view and map to it but I am unable to. I'm getting a 404 error.
The app inside my project is called emails.
Here are the files:
base/urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^emails/$', include('emails.urls')),
)
emails/urls.py
from django.conf.urls import patterns, include, url
from emails import views
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^custom/$', views.custom, name='custom'),
)
emails/views.py
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello world, you're at the index.")
def custom(request):
return HttpResponse("This is a custom view.")
Here is the 404:
Using the URLconf defined in crm.urls, Django tried these URL patterns, in this order:
^admin/
^/emails/$
The current URL, emails/custom, didn't match any of these.
When I visit localhost:8000/emails/ - I see the index view. However localhost:8000/emails/custom/ is returning a 404 error. Any help would be appreciated!
This url(r'^emails/$', include('emails.urls')), should be without the dollar sign $:
url(r'^emails/', include('emails.urls')),
You don't want to end your path after emails, so don't end the string with the regex character $. Let it be able to be continued in the other urls.py file

Getting a 404 when trying to access a url python/django

Currently getting a 404 error for my application when I try to access the url /contato, and I am not sure why, can anyone help me?
Here is the error:
Page not found (404)
Request Method: GET
Request URL: http://localhost:8002/contato
(...)
^contato/
(...)
Here is my code.
My main urls.py has:
from django.conf.urls import patterns, include, url
from django.conf import settings
from django.contrib import admin
urlpatterns = patterns('',
url(r'^admin/password_reset/$', 'django.contrib.auth.views.password_reset', name='admin_password_reset'),
url(r'^admin/password_reset/done/$', 'django.contrib.auth.views.password_reset_done'),
url(r'^reset/(?P<uidb36>[0-9A-Za-z]+)-(?P<token>.+)/$', 'django.contrib.auth.views.password_reset_confirm'),
url(r'^reset/done/$', 'django.contrib.auth.views.password_reset_complete'),
url(r'^admin/', include(admin.site.urls)),
url(r'^perfil/', include('perfil.urls')),
url(r'^contato/', include('contato.urls')),
url(r'^$', 'views.index', name='index'),)
My contato.urls.py:
from django.conf.urls.defaults import patterns, url
urlpatterns = patterns('contato.views',
url(r'^contato/$', 'contato', name="contato"),
)
my views.py in contato:
from django.shortcuts import render_to_response
from django.template import RequestContext
def contato(request):
render_to_response('contato/contato.html',locals(),context_instance=RequestContext(request),)
You don't have a match for the url "/contato". In your base urls.py, you point the prefix "/contato" to include contato.urls, and then in that file you have a single URL which is again "/contato": so the combined URL for that view is "/contato/contato".
If you just want the URL to match "contato", you should either get the included url to match just "^$", or (probably better) don't bother including the separate urls.py and match the view directly from the base file.

Django 1.5 "Page not found" with GET parameters in url

I have little trouble with Django. It cannot recognise urls with get params like this:
http://example.com/login/?next=/me/registrace/
I suppose it is due to bad settings, but I dont know where. The same problem is even in administration.
This is what I see on error page:
Using the URLconf defined in example.urls, Django tried these URL patterns, in this order:
^admin/
^imperavi/
^login/$ [name='login']
^$ [name='index']
The current URL, login/, didn't match any of these.
My urls.py
from django.conf.urls import patterns, include, url
from django.utils.translation import ugettext_lazy as _
from django.contrib import admin
from example.views import IndexView, LoginView
admin.autodiscover()
urlpatterns = patterns('',
url(_(r'^admin/'), include(admin.site.urls)),
url(_(r'^imperavi/'), include('imperavi.urls')),
url(_(r'^login/$'), LoginView.as_view(), name='login'),
url(_(r'^$'), IndexView.as_view(), name='index'),
)
I tried to remove ugettext translations, but it didnt help.
I also tried to remove all my rules and let there just admin, but still it doesn't work. I tried admin url:
http://example.com/admin/auth/user/?o=-1
There is a slash missing in the last url:
...
^$ [name='index']
The current URL, login/, didn't match any of these.
There is it: your rule should be url(_(r'^/$'), IndexView.as_view(), name='index'),

Django url Routing Error

I have a very basic url router in my django project:
from django.conf.urls import patterns, include, url
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.contrib import admin
admin.autodiscover()
urlpatterns = staticfiles_urlpatterns()
urlpatterns += patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^/?', include('customApp.urls')),
)
When I start the dev server and go to 127.0.0.1:8000/admin/, I get a ViewDoesNotExist at /admin/ error.
Here is the content of the exception:
Could not import customApp.views.event. View does not exist in module customApp.views.
I've already tried re-ordering the urls (I have no idea how that would help, but I tried it anyway) and changing r'^/?' to r'^/'.
When I comment out the last url, the admin page works again.
Here's the customApp.urls code:
from django.conf.urls import patterns, include, url
import django.contrib.auth.views
import django.contrib.auth
urlpatterns = patterns('customApp.views',
url(r'^$', 'index'),
url(r'^rest/v1/event/add/$', 'event'),
url(r'^rest/v1/reports/$', 'reports'),
)
urlpatterns += patterns('',
url(r'^login/$', 'django.contrib.auth.views.login', {'template_name': 'login.html'}),
)
It is simple buddy. Django cannot find customApp views. Please ensure whatever views you have got in urls.py, it should exist.
Thanks

Categories