Django Post detail url url not found - django

Django Post list/detail urls, domain.com/api/v1/1 and domain.com/api/v1 not found given the following url patterns, note that pk with value of 1 exists,
Project urls
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('api/v1/', include('posts.urls'))
]
Api urls
from django.urls import path
from .views import PostList, PostDetail
urlpatterns = [
path('<int:pk>/', PostDetail.as_view()),
path('', PostList.as_view()),
]

Try adding a slash to the end of api/v1
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('api/v1/', include('posts.urls'))
]

Related

Why is the route correct but still giving 404 error on Django

I got a 404 error on my website when accessing the correct route, did I do something wrong
urls.py handles the main route
from django.contrib import admin
from django.urls import path,include,re_path
urlpatterns = [
path('admin/', admin.site.urls),
re_path(r'api/facebook/(.+?)', include('apifacebooks.urls')),
path('', include('app.urls')),
path('getcookie', include('app.urls')),
path('change-lang', include('app.urls')),
re_path(r'auth/(.+?)', include('app.urls')),
]
urls.py handles routes in the apifacebooks app
from django.urls import path
from . import views
urlpatterns = [
path('api/facebook/like-post',views.like_post)
]
And when I go to http://localhost:8000/api/facebook/like-post I get a 404 error
Image error 404
My question has been solved, thanks
In your apifacebooks app change the path because you had "api..." double in the path
from django.urls import path
from . import views
urlpatterns = [
path('like-post/',views.like_post)
]
And in the root urls.py just
path('api/facebook/', ....)
In your code, the url pattern would be "http://localhost:8000/api/facebook/api/facebook/like-post".
As explained id Django docs:
Whenever Django encounters include(), it chops off whatever part of
the URL matched up to that point and sends the remaining string to the
included URLconf for further processing.
Remove "api/facebook/" in "apifacebooks.urls", for example:
Main urls.py
from django.contrib import admin
from django.urls import path,include,re_path
urlpatterns = [
path('admin/', admin.site.urls),
re_path(r'api/facebook/(.+?)/', include('apifacebooks.urls')),
path('', include('app.urls')),
path('getcookie', include('app.urls')),
path('change-lang', include('app.urls')),
re_path(r'auth/(.+?)', include('app.urls')),
]
apifacebooks.urls
from django.urls import path
from . import views
urlpatterns = [
path('like-post',views.like_post)
]
Or you can try to place remove "r'api/facebook/(.+?)'", for example:
from django.contrib import admin
from django.urls import path,include,re_path
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('apifacebooks.urls')),
path('', include('app.urls')),
path('getcookie', include('app.urls')),
path('change-lang', include('app.urls')),
re_path(r'auth/(.+?)', include('app.urls')),
]

Running into 404 error with URL Dispatcher

This is my url.py in WebFetcher
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('', include('Fetcher.urls')),
path('admin/', admin.site.urls),
]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
This is my url.py in Fetcher
from django.urls import path
from . import views
urlpatterns = [
path('', views.home, name = 'home'),
path('page_objects/', views.page_objects, name = 'page_objects')
]
this is my form
<form action="{% url 'page_objects' %}" method="post" enctype="multipart/form-data">
This is the name of my function in views
def page_objects(request):
I am getting 404 error saying
Using the URLconf defined in WebFetcher.urls, Django tried these URL patterns, in this order:
[name='home']
page_ojects [name='page_objects']
admin/
^media/(?P.*)$
The current path, WebFetcher/page_ojects, didn't match any of these.
I ready all the documentation on the URL Dispatcher and I could not find anything that looks wrong with my code. I hope it is just a syntax error. If you think more of my code will be helpful, comment and I will edit this post.
Edit 1:
I updated my WebFetcher urls.py and my Fetcher urls.py per Daniel's suggest.
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
from Fetcher import views
urlpatterns = [
path('WebFetcher/', include('Fetcher.urls')),
path('', views.home, name = 'home'),
path('admin/', admin.site.urls),
]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
from django.urls import path
from . import views
urlpatterns = [
path('page_objects/', views.page_objects, name = 'page_objects')
]
Now the 404 error I am getting is
Page not found (404)
Request Method: POST
Request URL: http://127.0.0.1:8000/WebFetcher/WebFetcher/WebFetcher/page_objects/
Using the URLconf defined in WebFetcher.urls, Django tried these URL patterns, in this order:
WebFetcher/ page_objects/ [name='page_objects']
[name='home']
admin/
^media/(?P.*)$
The current path, WebFetcher/WebFetcher/page_objects/, didn't match any of these.
You need to go to http://mywebsite/page_objects instead of http://mywebsite/WebFetcher/page_objects.
If you want to have the page_objects url nested under WebFetcher then you can do this to your urls.py:
MyProject/urls.py:
urlpatterns = [
path('WebFetcher/', include('Fetcher.urls')), # add the prefix here
path('admin/', admin.site.urls),
]
Fetcher/urls.py:
urlpatterns = [
path('', views.home, name = 'home'),
path('page_objects/', views.page_objects, name = 'page_objects')
]
Note: your home page will now be at http://mywebsite/WebFetcher - if you want it to be at the root, i.e. http://mywebsite then you can do this instead:
MyProject/urls.py:
urlpatterns = [
path('WebFetcher/', include('Fetcher.urls')), # add the prefix here
path('', views.home, name = 'home'), # move the home page path to the root urls.py
path('admin/', admin.site.urls),
]
Fetcher/urls.py:
urlpatterns = [
path('page_objects/', views.page_objects, name = 'page_objects')
]
You have a typo in your urls.py path.
page_ojects should be page_objects
please write this pattern that will work fine.
path('page_ojects/', views.page_objects, name = 'page_objects')
Updated: add this code to your project level urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('Fetcher.urls')),
]
And if it doesn't solve your issue, then there might a issue in your views.py module, maybe your views doesn't find any correct views to dispatch. so write below code for testing purpose,in yours views.py
from django.shortcuts import render
def page_objects(request):
return render("Hello, pages_objects!!")

django.urls.exceptions.NoReverseMatch: 'admin' is not a registered namespace

I have a Django projects with two apps, "projects" and "codebox", they were running fine, then at some point I got the following error:
django.urls.exceptions.NoReverseMatch: 'admin' is not a registered
namespace
If I remove the link to my admin panel from my template this error goes away, but then I can't get to my admin panel:
Admin
I was working in the urls.py files when this error occurred, have I changed something that is inadvertently having an impact on the admin link?
Here is my top level urls.py file:
from django.contrib import admin
from django.urls import include, path, re_path
urlpatterns = [
# path('', include('projects.urls')),
path('projects/', include('projects.urls')),
re_path('^accounts/', include('django.contrib.auth.urls')),
re_path('^logged_out/', include('projects.urls')),
path('codebox/', include('codebox.urls')),
]
Here is my urls.py for "projects":
from django.urls import path, include
from . import views
app_name = 'projects'
urlpatterns = [
path('', views.IndexView.as_view(), name='index'),
path('<int:pk>/', views.DetailView.as_view(), name='detail'),
path('insight/', views.InsightView.as_view(), name='insight'),
path('logged_out/', views.LoggedoutView.as_view(), name='loggedout'),
path('insight/subgen/', views.SubgenView.as_view(), name='subgen'),
]
And here is urls.py for my second app, codebox:
from django.urls import path, include
from . import views
app_name = 'codebox'
urlpatterns = [
path('', views.IndexView.as_view(), name='index'),
path('<int:pk>/', views.DetailView.as_view(), name='detail'),
path('form/', views.CreateView, name="form"),
]
add this in top of project/urls.py
from django.conf.urls import include, url
then add this in the url_pattern
url(r' ', include(('<app_name>.urls','<app_name>'), namespace='<app_name>')),
hope this will work
Check your base html file which you're extending to other html pages. If there is some space between urls tag remove that space and save your file and try again you can check below code for ref.
from django.contrib import admin
from django.urls import path, include
from passapp import views
urlpatterns = [
path('', views.index, name='index'),
path('admin/', admin.site.urls),
path('passapp/', include('passapp.urls')),
]
href="{% url 'admin:index' %}"

django url is giving page not found (404) error

I am getting page not found(404) error for votings app that I created from datacamp tutorial. I have checked my code to make sure it's free of errors. admin is working fine but other urls are not.
Here's urls.py code from the main application directory:
from django.urls import include, path
from django.contrib import admin
urlpatterns = [
path('blog/', include('blog.urls')),
path('votings/',include('votings.urls')),
path('admin/', admin.site.urls),
]
Here's urls.py from the votings app directory:
from django.urls import path
from . import views
urlpatterns = [
path('',views.index, name='index'),
path('<int:question_id>/',views.detail, name='detail'),
path('<int:question_id>/results/', views.results, name='results'),
path('<int:question_id>/vote/', views.vote, name='vote'),
]
I am using django 2.0.5.
Thanks
Unless you've made a mistake copying the wrong urls.py for votings app, the problem has to be it.
This is the main urls.py of your project:
from django.urls import include, path
from django.contrib import admin
urlpatterns = [
path('blog/', include('blog.urls')),
path('votings/',include('votings.urls')),
path('admin/', admin.site.urls),
]
FYI, according to the docs include() adds urls from your app directory's (in your case it's voting) urls.py to the main urls.py (in memory). This keeps the main urls.py from getting too big to read.
And this is the urls.py of your votings app which is literally the copy of main urls.py:
from django.urls import include, path
from django.contrib import admin
urlpatterns = [
path('blog/', include('blog.urls')),
path('votings/',include('votings.urls')),
path('admin/', admin.site.urls),
]
Don't you see any problem here? There's no endpoint. Where's the associated view (function-based or class based) for this url?
I suggest writing a view in your views.py and test it out:
Votings app views.py:
from django.http import HttpResponse
import datetime
def current_datetime(request):
now = datetime.datetime.now()
html = "<html><body>It is now %s.</body></html>" % now
return HttpResponse(html)
Votings app urls.py:
from django.urls import include, path
from . import views
urlpatterns = [
path('home/', views.current_datetime, name='home'),
]

Error W005 URL namespace isn't unique

'Copied this code from the Django tutorial into my app's urls.py file...
from django.urls import path
from . import views
app_name = 'polls'
urlpatterns = [
path('', views.index, name='index'),
path('<int:question_id>/', views.detail, name='detail'),
path('<int:question_id>/results/', views.results, name='results'),
path('<int:question_id>/vote/', views.vote, name='vote'),
]
When I start my server it produces the following error...
(urls.W005) URL namespace 'polls' isn't unique. You may not be able
to reverse all URLs in this namespace
I've tried using some other name besides 'polls' but with the same result. What am I doing wrong?
Check your root urls file, and be sure to have unique name:
(django 2+)
For example:
mysite/urls.py
-->
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('', include('polls.urls')), #polls.urls is unique
path('admin/', admin.site.urls), #admin.site.urls is unique
]
from django.conf.urls import url
import views
urlpatterns = [
url(r'^$', views.index,name='index'),
url(r'^(?P<question_id>[0-9a-f-]+)/$',views.detail,name='detail'),
url(r'^(?P<question_id>[0-9a-f-]+)/results/$',views.results,name='results'),
url(r'^(?P<question_id>[0-9a-f-]+)/vote/$',views.vote,name='vote'),
]
Write your url file like shown above.