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
Related
I'm a beginner Django. So I read the tutorials and I'm following them.
But I'm receiving an Error page. 'What is the problem...?'
(I installed the python 3.6.5 version)
My error:
Using the URLconf defined in mysite.urls, Django tried these URL
patterns, in this order:
1.polls/
2.admin/
The empty path didn't match any of these.
polls/urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
polls/views.py
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, world. You're at the polls index.")
And Finally!!
mysite/urls.py
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('polls/', include('polls.urls')),
path('admin/', admin.site.urls),
]
But running python manage.py runserver gives this error:
Page not found !!, The empty path didn't match any of these!!!
So I need your help, plz help me....!!
I assume your mysite is the "Django project name", so it contains the root urls.py. If we take a look at the file, we see:
# mysite/urls.py
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('polls/', include('polls.urls')),
path('admin/', admin.site.urls),
]
So that means that all the urlpatterns in the polls/urls.py are "sub URLs" of this path('polls/', ...), therefore you can say that the URLs in polls/urls.py are "prefixed" with 'polls/' implicitly.
In order to reach the index(..) view, we thus have to find a "path" from the root URLs to this specific view. The only way to reach this is first taking the 'polls/' path, and then selecting the '' path in polls/urls.py. The path is thus polls/.
So in order to "trigger" this view, you need to query a URL like:
https://localhost:8000/polls/
(of course if you have specified another port, or run the Django webserver on another server, you need to modify localhost, and 8000 accordingly).
I stumbled on this error and figured out that I misinterpreted the instruction at the top of page 18, section 2.3.4, listing 10. I modified that mysite/usrls.py file at the top level mysite directory, instead of the mysite/mysite/urls.py file. It worked fine after that.
I'm stuck with a Django project, I tried to add another app called login to make a login page but for some reason the page just redirects to the homepage except for the admin page
For example: 127.0.0.1:8000 will go to the homepage but 127.0.0.1:8000/login will also display the homepage even though I linked another template to it.
Here is my code:
main urls.py
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^', include('portal.urls')),
url(r'^login/', include('login.urls')),
]
login urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^login/', views.index, name="login"),
]
login views.py
from django.shortcuts import render
def index(request):
return render(request, 'login/login.html')
portal urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^', views.index, name="portal"),
]
I see 2 problems here:
As #DanielRoseman mentioned above, the regular expression ^ matches anything, so you should change it to ^$.
When you use an include, the rest of the path after what the include matched is passed to the included pattern. You’ll want to use ^$ in your login urls.py too.
You don't terminate the portal index URL, so it matches everything. It should be:
url(r'^$', views.index, name="portal"),
In addition, if the regex is login/$ but you enter http ://server/login, then it won't match wheras http://server/login/ will.
You could try changing the regex to login/*$, which will match any number (even zero) / on the end of the url.
So http: //server/login, http: //server/login/, http: //server/login//// would all match.
Or if you want to be specific, login/{0,1}$ might work (though that regex syntax is from memory!)
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 am extremely new to programming. I searched and found a few similar threads here, though unfortunately the answers in those threads didn't seem to fix my own issue.
Problem
I am working my way through the Django tutorials, and I am on the third tutorial:
https://docs.djangoproject.com/en/1.7/intro/tutorial03/
I'm not sure if this matters, but I am working on a Macintosh w/ Mavericks.
The root directory in which I am working is 'Test2'. The directory structure is:
/Users/me/src/collact/collact/test2/
Within 'test2', and per the previous two tutorials, I have '/polls' and '/templates' folders.
My polls/views.py file has the following text:
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, world. You're at the polls index.")
My polls/urls.py file has the following text:
from django.conf.urls import patterns, url
from polls import views
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
)
Finally, in /test2/, I have a urls python file as well (.py). It has the text:
from django.conf.urls import patterns, include, url
from django.contrib import admin
urlpatterns = patterns('',
url(r'^polls/', include('polls.urls')),
url(r'^admin/', include(admin.site.urls)),
)
As far as I can tell, this is what the tutorial prescribes. Unfortunately, when I run the server using: python manage.py runserver, the website gives me an error/404 message:
Page not found (404)
Request Method:
GET Request
URL: http://localhost:8000/polls/ Using the URLconf defined in
test2.urls, Django tried these URL patterns, in this order: ^admin/
The current URL, polls/, 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.
Does anyone have any idea how I can fix this? I know that this is a really stupid question, but I have been trying to figure it out for the past day or two, and unfortunately I'm just not familiar enough with programming to solve this on my own.
I'm developing a webpage with Django.
I want to add/define an url for the application.
At this moment the url (dsvd/) doen't work properly. It shows only table data but no css and background.
here is the code for the main urls.py file.
from django.conf.urls import patterns, include, url
from django.conf import settings
from django.conf.urls.static import static
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'kleedkamer_overview.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'^', include('kleedkamer_overview.urls')),
url(r'^dsvd/', 'kleedkamer_overview.views.indeling'),
)
and here the code inside the application urls.py file
from django.conf.urls import patterns, url
from kleedkamer_overview import views
urlpatterns = patterns ('',
url(r'^$',views.indeling, name='indeling'),
)
Is there anyone who can help me find the problem?
Greetz.
It looks like line is giving you trouble:
url(r'^dsvd/', 'kleedkamer_overview.views.indeling')
It seems like you are trying to map this URL to kleedkamer_overview.views.indeling but the URL regrex does not end with a '$'. Urls.py files that are mapped to other apps look like this:
url(r'^admin/', include(admin.site.urls))
Notice the include function call and that the regrex expression r'^admin/' does not end with a $
However mapping to a specific view is a bit different and it looks like this:
url(r'^$', 'kleedkamer_overview.views.home', name='home')
Notice that this time, where the include() function call would go you are instead telling Django which specific view you want to use and that the site regrex is r'^$' ending with a $.
Try changing this:
url(r'^dsvd/', 'kleedkamer_overview.views.indeling')
to this:
url(r'^dsvd/$', 'kleedkamer_overview.views.indeling')
Edit:
I saw your comments and while the urls.py is not the source of your problem, what I said is still valid because the urls.py was malformed. You should not even have the offending line there at all because you have already included the urls.py from kleedkamer_overview , there is no need to include it twice. It isn't DRY and it is just bad practice in general. This is why this still works despite being malformed because sites are looked for in order, in this case first it looks for /admin then / and does not reach /dsvd because it was already caught by / and your malformed url mapping is NEVER reached.