Routing error: trying to route a defined route in urls.py - django

I encountered a strange behavior of Django urls. Although forecast/upload url is defined in the urls.py, Django is saying
Page not found (404)
Request Method: POST
Request URL: http://localhost:8000/forecast/upload/
Using the URLconf defined in myproject.urls, Django tried these URL patterns, in this order:
polls/
admin/
forecast/ [name='index']
forecast/ **forecast/upload/** [name='upload']
forecast/ help.html [name='help']
The current path, **forecast/upload/**, didn't match any of these.
Project url.py:
from django.contrib import admin
from django.urls import include, path
from django.conf.urls import url
urlpatterns = [
path('polls/', include('polls.urls')),
path('admin/', admin.site.urls),
path('forecast/', include('forecast.urls')),
]
Application urls.py:
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
#url(r'^$', views.home, name="home"),
path('forecast/upload/', views.upload, name="upload"),
path('help.html', views.help, name="help"),
]

You have specified "forecast" twice; once at project level and once in the app. So your URL would be "forecast/forecast/upload".
Presumably, you don't want that, in which case you should remove the "forecast" from the pattern in the app urls.

Related

Using the URLconf defined in myapp.urls, Django tried these URL patterns, in this order

I think I went through every available answer to this error but they all reference some other tutorial and a views.py which I do not have. After running the tutorial from here I get this 404 error.
Using the URLconf defined in myapp.urls, Django tried these URL patterns, in this order:
admin/
^$ [name='home']
^media/(?P<path>.*)$
^static/(?P<path>.*)$
The current path, market_data/, didn’t match any of these.
The urls.py has the following code:
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
from django.urls import re_path as url
from django.views.generic.base import TemplateView
urlpatterns = [
path('admin/', admin.site.urls),
url(r'^$', TemplateView.as_view(template_name='templates/static_pages/index.html'), name='home'),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
The root directory structure of this project is
~/market_data/myapp
~/market_data/myapp/static_media
~/market_data/myapp/static_files
~/market_data/myapp/static_files/admin
~/market_data/myapp/templates
~/market_data/myapp/templates/static_pages
Thank you for any tips!

Django url can't work after i move it down from the list

I am making a blog app using Django. In my blog app, i have 6 paths. This is my app urls.py
from . import views
from .views import AddPostView
from django.urls import path
from .feeds import LatestPostsFeed
urlpatterns = [
path('', views.PostList.as_view(), name='home'),
# path('<slug:slug>/', views.PostDetail.as_view(), name='post_detail'),
path('upload/', views.image_upload_view, name="upload"),
path('add_post/', AddPostView.as_view(), name='add_post'),
path('<slug:slug>/', views.post_detail, name='post_detail'),
path("feed/rss", LatestPostsFeed(), name="post_feed"),
]
The app is working perfectly, but whenever i try to move the path('<slug:slug>/', views.post_detail, name='post_detail') up from the add_post url (code below) and try to open add_post
from . import views
from .views import AddPostView
from django.urls import path
from .feeds import LatestPostsFeed
urlpatterns = [
path('', views.PostList.as_view(), name='home'),
# path('<slug:slug>/', views.PostDetail.as_view(), name='post_detail'),
path('upload/', views.image_upload_view, name="upload"),
path('<slug:slug>/', views.post_detail, name='post_detail'),
path('add_post/', AddPostView.as_view(), name='add_post'),
path("feed/rss", LatestPostsFeed(), name="post_feed"),
]
suddenly i have an error saying :
Page not found (404)
No Post matches the given query.
Request Method: GET Request URL: http://localhost:8000/add_post/
Raised by: blog.views.post_detail
Using the URLconf defined in mysite.urls, Django tried these URL
patterns, in this order:
admin/
[name='home']
upload/ [name='upload']
<slug:slug>/ [name='post_detail']
The current path, add_post/, matched the last one.
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.
I don't understand why it happens and i am curious and want to know what has just happened. maybe there are some concepts about django urls that i don't know. Can you explain to me what happen in this error?

The current path, index, didn't match any of these. http://127.0.0.1:8001/index

I setup webpage via cmd for Django.However, i had problem when accessing index page of 127.0.0.1:8001. I tried to update urls.py but still have problem.
Browser page error:
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8001/index
Using the URLconf defined in barry.urls, Django tried these URL patterns, in this order:
admin/
The current path, index, didn't match any of these.
urls.py file:
"""{{ project_name }} URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/{{ docs_version }}/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
from django.conf.urls import url
from sign import views
urlpatterns = [
path('admin/', admin.site.urls),
path('index/', views.index),
]
views.py
from django.shortcuts import render
# Create your views here.
def index(request):
return HttpResponse("Hello Django!")
You are mistaking in path pattern. Confusing with path, re_path and url.
Have a look at documentation Django Urls - Path.
urlpatterns = [
path('admin/', admin.site.urls),
path('index/', views.index),
]

Django page not found error

Hello I am trying to solve the error which keeps on occurring;
my current code on mysite/urls.py is
from django.contrib import admin
from django.urls import path
from django.conf.urls import url
from polls import views
admin.autodiscover()
urlpatterns = [
url(r'^polls/$', views.index, name='index'),
url(r'^polls/(?P<question_id>\d+)/$', views.detail, name='detail'),
url(r'^polls/(?P<question_id>\d+)/vote/$', views.vote, name='vote'),
url(r'^polls/(?P<question_id>\d+)/results/$', views.results, name='results'),
url('admin/', admin.site.urls),
]
and I am getting error when I try to access :8001/polls
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8001/
Using the URLconf defined in mysite.urls,
Django tried these URL patterns, in this order:
^polls/$ [name='index']
^polls/(?P<question_id>\d+)/$ [name='detail']
^polls/(?P<question_id>\d+)/vote/$ [name='vote']
^polls/(?P<question_id>\d+)/results/$ [name='results']
admin/
The empty path didn't match any of these.
Please help me to solve this error which makes me cannot access to the website.

Django Rest Framework URL Mapping for multiple apps

I have a django-rest project called main and under it I have created an app called users. So, my project has the files :-
main/main/urls.py
and
main/users/urls.py
In users/urls.py I have
from django.conf.urls import url, include
from rest_framework import routers
from users import views
router = routers.DefaultRouter()
router.register(r'users', views.UserViewSet)
and in the main/main/urls.py I have
from django.conf.urls import url
from django.contrib import admin
from users import urls
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^users/', users.urls),
]
However, I keep getting the error NameError: name 'users' is not defined. What is the correct way to set up urls when I have multiple apps? I would like to have a urls.py file for each app that is independent of the project. And in the root urls.py would include routing to different apps.
You import url not user, can try it
from users import urls as users_url
# ^^^^^^^^
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^users/', users_url),
# ^^^^^^^
]
but better:
from django.conf.urls import url, include
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^users/', include('users.url')),
# ^^^^^^^
]
more details including-other-urlconfs