I have a django project mysite
and a djano app in a directory (proto) called http
and here is me url setting:
mysite/mysite/urls.py:
import proto
urlpatterns = patterns('',
url(r'^data/',proto.http.views.DataList.as_view(),name='data')
)
mysite/proto/http/urls.py:
from django.conf.urls import patterns, include, url
from proto.http import views
urlpatterns = patterns('',
url(r'^data/$',views.DataList.as_view(),name='data-list'),
)
So I can visit : http://127.0.0.1:8000/data/
When I reviewmy code ,I feel weird why I have to set the url twice?
But if I comment out the url pattern in mysite/proto/http/urls.py
#urlpatterns = patterns('',
# url(r'^data/$',views.DataList.as_view(),name='data-list'),
#)
I got 500 error
ImproperlyConfigured at /data/
The included urlconf '<module 'proto.http.urls' from '/Users/Winsome/mysite/proto/http/urls.pyc'>' does not appear to have any patterns in it. If you see valid patterns in the file then the issue is probably caused by a circular import.
What is the correct way to set url pattern ??
Related
I'm going through the polls Django tutorial and have searched for answers to this. So far:
I made sure I'm using the right directories. The main urls.py is in mysite/mysite/, the polls urls.py is in mysite/polls/urls.py.
Tried adding 'polls' to INSTALLED_APPS in the settings.py of mysite/mysite.
Am making sure that I am requesting 127.0.0.1:8000/polls and not 127.0.0.1:8000
I am using Python 3.4 and Django 1.9, same as the tutorial.
I am still receiving this message:
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/polls
Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
^polls/
^admin/
The current URL, polls, didn't match any of these.
mysite/mysite/urls.py
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^polls/', include('polls.urls')),
url(r'^admin/', admin.site.urls),
]
mysite/polls/urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^%', views.index, name='index'),
]
mysite/polls/views.py
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, world")
In your polls/urls.py
change url(r'^%', views.index, name='index') to url(r'^$', views.index, name='index').
Also in your urls.py, you have route for 127.0.0.1:8000/polls/ and you request for 127.0.0.1:8000/polls . Notice the trailing slash.
So you should request to 127.0.0.1:8000/polls/ .
Add APPEND_SLASH = True in your settings.py file so that it would redirect 127.0.0.1:8000/polls to 127.0.0.1:8000/polls/ .
So i tried your Index view with your urls.py.
It works, the only thing different is in your "Request URL, you must request for - 127.0.0.1:8000/polls/%
changing your urls to
url('^/%', index) - polls URLs.py
url('^polls', include('polls.urls')) - project URLs
This will work.
I followed the instructions in the tutorial
I made one change for debugging, here are the files:
mysite1\urls.py:
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^abc/', include('polls.urls')),
url(r'^polls/', admin.site.urls),
url(r'^admin/', admin.site.urls),
]
File: mysite1\polls\urls.py
from django.conf.urls import url
from .import views
urlpatterns = [
url(r'^&', views.index, name='index'),
]
Now if I go to the site http://127.0.0.1:8000/polls/ then it shows the login page same as going to the site http://127.0.0.1:8000/admin/.
However, if I go to the site http://127.0.0.1:8000/abc/ it gives me the following:
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/abc
Using the URLconf defined in mysite1.urls, Django tried these URL patterns, in this order:
^abc/
^polls/
^admin/
The current URL, abc, didn't match any of these.
You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.
Can someone guide me what I am doing wrong?
In mysite1\polls\urls.py It should be
url(r'^$', views.index, name='index'),
Notice that your code had an & instead of $. $ indicates a end-of-string match character.
I had a few issues with this, maybe it'll help someone. My VS code was running python 2.7 so the first line on page urls.py in the web project folder (not the polls folder) I had to change to "django.conf.urls import include, url" from "from "django.urls import include, path". I had to include .conf. and urls, path was not working and it was not working without "conf".
Another issue I had on this same page is I had to correct it to "urlpatterns = [url('', include('hello.urls'))..." from "urlpatterns = [path('', include("hello.urls"))". Path was not working so I got this to work.
The third thing is, Notice that there is a '' in the code. It should be blank otherwise when you go to url http://127.0.0.1:8000/ it doesn't redirect to views.py in the polls folder.
I hope this helps someone. Thanks
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.
I am newbie in django framework
My project urls.py has the following code
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^rango/$',include(rango.urls)), #my test appliaction
url(r'^admin/', include(admin.site.urls)),
)
I have created an app called rango , that i have imported in the main project url file.
Now it throwing the following error, when I am trying to access url .../rango/
Exception Value: name 'rango' is not defined
I can see that in that python path is not correctly set up.
This is the directory structure of my project
project/
project/__init__.py
project/urls.py
rango/__init__.py
valuable advice required.
url(r'^rango/$',include(rango.urls)),
should read:
url(r'^rango/',include('rango.urls')),
No $ and use quotes 'rango.urls'. Because rango without the quotes is not defined.
I'm troubleshooting an issue with my urls.py file and I just noticed that the django default 404 page is returning "No Post matches the given query." in the body rather than
"Using the URLconf defined in X, Django tried these URL patterns in this order:
^admin/
^$
...
The current URL, myurl/ didn't match any of these."
Is this a toggle somewhere? I know I've seen it before...I'm using Django 1.5.1, DEBUG = TRUE is set in settings.py and I have other URLs defined in urls.py:
from django.conf.urls import patterns, include, url
from django.contrib import admin, flatpages
from django.conf import settings
admin.autodiscover()
urlpatterns = patterns('',
# Uncomment the admin/doc line below to enable admin documentation:
url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'^$', 'blog.views.index'),
url(r'^(?P<slug>[\w\-]+)/$', 'blog.views.post'),
url(r'^contact/$', 'contact.views.contact'), #troubleshooting this one
)
I think that this is because you get the error because you use the shortcut function get_object_or_404 with Post model.
So the urls patterns work ok. Try to locate where you use this function and add some printing or break with pdb.