I used the official Django documentation to create this code and I consistently get a 404 error when I put localhost:server/polls/ in the search bar. I followed the tutorial pretty much identically but it still doesn't seem to work. Anyone know a solution to get text displayed at localhost:server/polls/ or at localhost:server?
polls/views.py:
from django.shortcuts import render
from django.http import HttpResponse
def index(response):
return HttpResponse('Polls')
polls/urls.py:
from django.urls import path
from . import views
urlpatterns =[
path('', views.index, name='index'),
]
mysite/urls.py:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('polls/', include('polls.urls')),
]
Error message:
Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order: admin/
The current path, polls/, didn't match any of these.
I have looked at lots of different potential solutions and none of them seem to work. Please let me know if you need more info/code, thanks
Related
app urls.py
from django.urls import path
from . import views
urlpatterns = [
path('hello', views.index, name='index')
]
project urls.py
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('admin/', admin.site.urls),
path('hello/', include("hello.urls"))
]
views.py
from django.http import HttpResponse
from django.shortcuts import render
# Create your views here.
def index(request):
return HttpResponse("Hello, World!")
I've re-watched the tutorial several times and still can't find the error.
Since your project's urls.py includes hello using the prefix 'hello/' in
path('hello/', include("hello.urls"))
and your app's urls.py also has hello as the path in
path('hello', views.index, name='index')
you will need to access /hello/hello in your browser to get to a view that doesn't 404.
Django's technical 404 page (the one with the yellow background) should show the available paths -- if you don't get the technical 404 page, ensure you have the DEBUG setting turned on.
I am following this tutorial.
https://www.youtube.com/watch?v=a48xeeo5Vnk
Here is my code for views:
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def home(request):
return HttpResponse('<h1>This is our blog</h1>')
def about(request):
return HttpResponse('<h1>This is our About Page</h1>')
def next(request):
return HttpResponse('<h1>This is our next Page</h1>')
This is my App urls page code
from django.urls import path
from . import views
urlpatterns = [
path('', views.home, name='blog-home'),
path('about/', views.about, name='blog-about'),
path('next/', views.next, name='blog-NEXT'),
]
This is my main project URL code
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('firstapp', include('firstapp.urls')),
path('admin/', admin.site.urls),
]
Now when I try this with only single view i.e default page '', it works alone, with no more pages mentioned in code, however when I add more views it gives me a 404 page error. I believe the code is fine and it shoud work, but somehow it chose not to.
Tried different browser, tried forums but nothing.
Here is the error.
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/firstapp/
Using the URLconf defined in first.urls, Django tried these URL patterns, in this order:
firstapp [name='blog-home']
firstapp about/ [name='blog-about']
firstapp next/ [name='blog-NEXT']
admin/
The current path, firstapp/, didn't match any of these.
Any assistance would be appreciated.
Do below changes
from:
path('firstapp', include('firstapp.urls')),
to:
path('firstapp/', include('firstapp.urls')),
I started working with Django yesterday and I have a little error. I searched on the internet but I cant find the answer for my problem. I made the main app and then I make a little app and tried to link it to the main. Here is my code
urls.py
from django.contrib import admin
from django.urls import path, include
ulrpatterns = [
path('admin/', admin.site.urls),
path('^$', include('personal.urls'));
]
in my personal.urls I have
from django.urls import path, include
from . import views
ulrpatterns = [
path('^$', views.index, name="index");
]
and in views.index I have
def index(request):
return render(request, 'personal/home.html')
Any help would be awesome, thank you!!!
You are using the wrong tool here: since django-2.0, there are basically two ways to specify a URL:
with a regex through re_path [Django-doc], url [Django-doc] is an "alias" of this, but will probably eventually get deprecated; and
by using a specific "path pattern" syntax with path [Django-doc]
Here you somehow mixed the two, and use path(..) function for a "regex-specified" URL.
Using path for path pattern-based URLs
We can also fix it by using path:
# urls.py
from django.contrib import admin
from django.urls import path, include
ulrpatterns = [
path('admin/', admin.site.urls),
path('', include('personal.urls'));
]
and:
# personal/urls.py
from django.urls import path, include
from . import views
ulrpatterns = [
path('', views.index, name="index");
]
Using re_path for regular expression-based URLs
We can also fix it by using re_path instead:
# urls.py
from django.contrib import admin
from django.urls import re_path, include
ulrpatterns = [
re_path('admin/', admin.site.urls),
re_path('^$', include('personal.urls'));
]
and:
# personal/urls.py
from django.urls import re_path, include
from . import views
ulrpatterns = [
re_path('^$', views.index, name="index");
]
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 have this code in one file called urls.py
from django.conf.urls import url
from django.contrib import admin
from myblog import urls
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'', urls),
]
Im trying to redirect to another file in a module called myblog which contains another file called urls.py with the following code:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.post_list, name='post_list'),
]
my views file which contains post_list method is as follows:
from django.shortcuts import render
def post_list(request):
return render(request,'myblog/post_list.html', {})
The result is that i keep getting the following error message:
module 'myblog.urls' from 'path\to\python\file\urls.py' is not a callable or a dot-notation path
Can anyone please explain to me where i am getting it wrong. I am new to django and python and i am using a tutorial from DjangoGirls.com. They use Django 1.8 and i have Django 1.9 installed.
Looking at the tutorial, uou can see that you have forgotten to use include.
First you have to add the import
from django.conf.urls import include, url
Then change the url pattern to:
url(r'', include('blog.urls')),
Be careful that you are using the correct module name - you have myblog, but the tutorial has blog.
You need to include the urls.py to link them
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'', include(urls, namespace='MyOtherapp')),
]
Note: I've also added a namespace here, its not required, it just helps when your using the url template tag.