Django: 1.8.5
Python: 3.5
I'm getting a 404 on just the webroot. I don't know what's wrong with my urlconf.
Directory structure:
myproject // from django-admin startproject
myproject // '' ''
__init__.py
settings.py
urls.py
views.py
myapp // from manage.py startapp
migrations
__init__.py
admin.py
models.py
tests.py
urls.py
views.py
In myproject > urls.py:
from django.conf.urls import include, url, patterns
from myapp import urls as app_urls
urlpatterns = patterns('',
url(r'^/$', include(app_urls)),
)
And in myapp > urls.py:
from django.conf.urls import include, url, patterns
urlpatterns = patterns('',
url(r'^/$', 'index'),
)
I run the django development server and I get a 404 on the index page, even though I have the empty regex defined.
Using the URLconf defined in myproject.urls, Django tried these URL patterns, in this order:
^/$
The current URL, , didn't match any of these.
The documentation in urls.conf from a a fresh install of django:
Including another URLconf
1. Add an import: from blog import urls as blog_urls
2. Add a URL to urlpatterns: url(r'^blog/', include(blog_urls))
Changing the regex in BOTH urls.py files to r'^' appears to work, but with a new 'Could not import 'index'. error.
Don't use $, when using include. Change it to:
# myproject/urls.py
from django.conf.urls import include, url, patterns
from myapp import urls as app_urls
urlpatterns = patterns('',
url(r'^', include(app_urls)),
)
# myapp/urls.py
from django.conf.urls import include, url, patterns
urlpatterns = patterns('',
url(r'^$', 'index'),
)
The root URL is ^$, not ^/$.
Also, when you include a urlconf from another urlconf, it uses both patterns together; it doesn't forget the original pattern. So your app is really looking for the url ^/$^/$, which I'm not sure can ever be matched.
Related
I am following the django tutorial (found here):
views.py code:
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, world. You're at the poll index.")
urls.py code (this is inside polls app urls.py file):
from django.urls import path, include
from django.conf import settings
from . import views
urlpatterns = [path(r'^$', views.index, name='index'), ]
urls.py code ( this is root urls.py file code):
from django.contrib import admin
from django.urls import include, path
urlpatterns = [path('polls/', include('polls.urls')),path('admin/',admin.site.urls), ]
Here is my run command : python manage.py runserver 8080
I tried to run it today, but I am getting the following error:
Page not found (404)
Request Method: GET
Request URL: http://35527a91f40c4e228d6c464d8a8c8487.vfs.cloud9.eu-west-1.amazonaws.com/
Using the URLconf defined in PollApp.urls, Django tried these URL patterns, in this order:
poll/
admin/
The empty path 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.
You are using regex-syntax with path, you should use the empty string instead:
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
Furthermore, you use a poll/ prefix here, so that means you either need to visit localhost:8000/poll/, or change this to an empty prefix:
urlpatterns = [
path('', include('polls.urls')),
path('admin/', admin.site.urls),
]
Djano errors are self-explanatory, follow its instructions, and are good to go.Also is there the Indentation in your first return ?
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
I am new to Django and I am trying to build an image classifier app, I just tried building the app but I am getting this error, how may I resolve it?
Page not found (404)
Request Method: GET
Request URL: http://localhost:8000/imageupload
Using the URLconf defined in imageclassifier.urls, Django tried these URL patterns, in this order:
imageupload ^$ [name='home']
admin/
The current path, imageupload, 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.
These are all changes I made in my app:
This is the views.py file in imgUpload app
from django.shortcuts import render
# Create your views here.
def home(request):
return render(request, 'home.html')
This is the urls.py file in imageUpload app
from django.contrib import admin
from django.urls import path,include
from . import views
urlpatterns = [
path('^$', views.home, name ='home'),
]
And this is the urls.py in the Imageclassifier folder:
"""imageclassifier URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/3.1/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path,include
urlpatterns = [
path('imageupload', include('imgUpload.urls')),
path('admin/', admin.site.urls),
]
Also, I have added a home.html file in the templates folder. How may I fix this error?
In the latest django path methods, You do not need to use ^$ in your path. Simply remove that ^$ from your path and it will work fine.
My project urls.py is as follows:
from django.conf.urls import include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^myapp/', include('myapp.urls')),
]
My app urls.py is as follows:
from django.conf.urls import patterns, include, url
urlpatterns = patterns('', url(r'^hello/', 'myapp.views.hello', name = 'hello'),)
Now , as soon as I try to run it , it gives me the following error:
from django.conf.urls import patterns, include, url
ImportError: cannot import name 'patterns'
Django doesn't require you to use patterns anymore, you can just make urlpatterns a list of urls
urlpatterns = [url(r'^hello/', 'myapp.views.hello', name = 'hello'),]
Django 1.11 loads that Python module and looks for the variable urlpatterns. This should be a Python list of django.conf.urls.url() instances.
And it runs through each URL pattern, in order, and stops at the first one that matches the requested URL
Dont require patterns more.
urlpatterns should be a Python list of url() instances.
So I am trying to call my url by using the following in one of my html templates -
<a href="{% url 'socialx:index' %}">
My apps urls.py file looks like this -
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^socialx/', include('socialx.urls')),
url(r'^admin/', include(admin.site.urls)),
]
And the root urls.py file is like this -
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^socialx/', include('socialx.urls'), name='socialx'),
url(r'^admin/', include(admin.site.urls)),
]
When navigating to the app via browser I get the following error -
NoReverseMatch at /socialx/ 'socialx' is not a registered namespace
Have a look at the documentation
I think that when you include the url in urls.py you need to add a namespace
url(r'^socialx/', include('socialx.urls', namespace="socialx")),
And make sure that the url for going to the index page has a name=index.
See if that helps.
I'm getting a 404 from Django and none of the previous posts on the subject (of which there are many) seem to have helped.
views.py
from django.views.generic.detail import DetailView, SingleObjectMixin
from app.models import MyModel
class MyDetails(DetailView, SingleObjectMixin):
template_name = "app/my_view.html"
model = MyModel
urls.py
from django.conf.urls import include, url
from django.contrib import admin
from app.views import MainList, post_form_upload, MyDetails
urlpatterns = [
url(r'^$', MainList.as_view(), name="main_list"),
url(r'^add_something$', post_form_upload, name="add_something"),
url(r'^my_details/(?P<pk>\d+)$', MyDetails.as_view(), name="my_details"),
]
app/urls.py
from django.conf.urls import url
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
(r'^$', include('app.urls')),
url(r'^admin/', include(admin.site.urls)),
]
when I enter the URL: http://localhost:8000/my_details, I get the following error:
Using the URLconf defined in app.urls, Django tried these URL patterns, in this order:
^$ [name='main_list']
^add_something$ [name='add_something']
^my_details/(?P<pk>\d+)$ [name='my_details']
The current URL, my_details, didn't match any of these.
The other two URLs (/ and /add_something) work fine.
First of all, not sure how you are not running into this issue, but in your app/urls.py
(r'^$', include('app.urls')),
should be
(r'^/', include('app.urls')),
$ indicates the end of regex pattern, and anything inside include() would not be included.
Secondly, None of your URL patterns match my_details/ They only match my_details/<id>
A little more on the documentation of URL regex, etc..