django - urlpatterns giving 404 error for defined URLs - python

I have an application running on django. But I am getting error code 404 for some urls even though these are defined.
from .views import check_secret_key # a function in views.py
from .swagger_schema import SwaggerSchemaView # a class inheriting APIView
from .kom.kom_writer import kom_status # a function
from django.conf.urls import url
urlpatterns = [
url(r'^docs/api-docs/', SwaggerSchemaView.as_view()),
url(r'^nag/secret-key/', check_secret_key),
url(r'^nag/kom-status/', kom_status),
]
API curl http://localhost:9999/internal/docs/api-docs/ works fine but curl http://localhost:9999/internal/nag/kom-status/ and nag/secret-key fir 404 errror.
Not FoundThe requested resource was not found on this server.
I am not sure what is it that I am missing.
Note: App was recently updated from DJango 1.8 to 1.11 and djangorestframework 3.2.5 to 3.5.3. Before that it was working fine.
For debugging purpose I am just returning success response right now.
from django.http import HttpResponse
def check_secret_key(request):
r = HttpResponse("I am good", content_type='text/plain', status=200)
return r

well, your definition should be something like
urlpatterns = [
url(r'.*docs/api-docs/', SwaggerSchemaView.as_view()),
url(r'.*nag/secret-key/', check_secret_key),
url(r'.*nag/kom-status/', kom_status),
]
also you need to provide /internal url definition.

I found the issue. Application is using two wsgi.py for running django apps.
Both wsgi.py files were using
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "it.settings.prod")
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "nt.settings.prod")
Changing it to
os.environ["DJANGO_SETTINGS_MODULE"] = "it.settings.prod"
os.environ["DJANGO_SETTINGS_MODULE"] = "nt.settings.prod"
resolved the problem.
Took reference from https://docs.djangoproject.com/en/3.1/howto/deployment/wsgi/modwsgi/

Related

Page not found 404 - Django

I'm a newbie to Django and I know this probably has been asked alot of times.
So basically what's happening is when I try to create a new project and whenever I'm trying to run my server, by default it's opening http://127.0.0.1:8000/catalog/ and not http://127.0.0.1:8000/.
Even if I run the server with my other projects, I'm facing the same error.
I followed this django basics tutorial on https://developer.mozilla.org/en-US/docs/Learn/Server-side/Django/skeleton_website
Idk but somehow I think it's default address is set to http://127.0.0.1:8000/catalog/.
Here's the link to the repo for the project:
https://github.com/Fanceh/django-404-error
Here's my project's urls.py:
from django.contrib import admin
from django.urls import path, include
from testuapp import urls
urlpatterns = [
path('admin/', admin.site.urls),
path('',include("testuapp.urls"))
]
Here's the code in my testuapp urls.py:
from django.urls import path
from . import views
urlpatterns = [
path('', views.testu),
]
Here's my webapp's views.py file:
from django.shortcuts import render
# Create your views here.
def testu(request):
render(request, 'Greetings!')
Is there any way I can change it?
Regards
From the tutorial link it mentions the redirect.
Any request for the root URL, will redirect you to /catalog.
Screenshot from the tutorial below.
HTH
so i assume you are below url pattern structure in your testuapp project.
urlpatterns = [
path('catalog',include("views.catalog"))
]
views.calalog is the name of the method in your view file.
Ok I think I figured it out, it's just the chrome cache. I cleared it and bam it's working!

Django cant be reached via parameterized url

Hello StackOverflow community,
I'm currently learning how to use the library Django combined with Python. However, I've ran into some issues which are somehow strange. My situation is the following. I have a project called "animals" which is the base of the Django application. My app is called "polls". Then I've defined the following views.
polls/views.py
from django.shortcuts import render
from django.http import HttpResponse
def animals(request, animal_id):
return HttpResponse("%s" % animal_id)
def index(request):
return HttpResponse('Hello world')
So far, so good. Unfortunatly I can't say the same about the urlpatterns.
polls/urls.py
from django.urls import path
from . import views
urlpatterns = [
path('',views.index,name='index'),
# Redirects to localhost:8000/polls/<animal_id>
path('<int:animal_id>/',views.animals,name='animals')
]
Whenever I want to navigate to the url "localhost:8000/polls/10/" Django reminds me of the fact, that the url is not accepted by the application and a 404 Error is thrown inside my browser. Am I missing something here?
UPDATE
I've managed to resolve the problem by fixing a rather trivial error. While the polls/urls.py was alright, the problem lay inside the animals/urls.py file. This file looked like this:
animals/urls.py
from django.conf.urls import url
from django.contrib import admin
from django.urls import include,path
urlpatterns = [
url(r'^admin/', admin.site.urls),
path('polls',include('polls.urls')),
]
As one can see, the "polls" path is not finished with a "/" sign. This indicates to Django that my desired route would be localhost:8000/polls where is any integer I add to the url. However, you have to add the slash at the end. Otherwise Django won't work as expected.

django-subdomains config localhost

I'm having trouble getting mine configured correctly.
Have sites enabled and site domain for "SITE_ID = 1" (db object 1 domain) set too "mysite.app"
I have these subdomains setup
ROOT_URLCONF = 'mysite.urls'
SUBDOMAIN_URLCONFS = {
None: 'frontend.urls', # no subdomain, e.g. ``example.com``
'www': 'frontend.urls',
'api': 'api.urls',
}
etc/hosts file
127.0.0.1 api.mysite.app
127.0.0.1 www.mysite.app
127.0.0.1 mysite.app
api/urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index),
]
api/views.py
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return HttpResponse('API')
The frontend app urls and views is identical except for returning string "FRONTEND" in the HttpResponse object.
I can tell django-subdomains is working becuase it does go to the "frontend" app when I hit "mysite.app:8000" vs the mysite.urls as seen in the root_url_conf. It displays "FRONTEND"
But no matter what I do I can't get "api.mysite.app:8000" to hit the api urls file to display "API"
Am I missing something? I'm very new to django. Any help is appreciated.
Thanks.
Simply had to restart the dev server. All was configured correctly.
Try http://api.127.0.0.1.xip.io:8000/ with your dev server running at 127.0.0.1:8000. Since 127.0.0.1 isn't really a domain but an IP, it can't have subdomains. And since your hosts file redirects to 127.0.0.1 not 127.0.0.1.xip.io or similar (you don't have to do it for testing either) you won't be connected.
I haven't used the library that you mentioned but from experience with self-written snippets for subdomains, I'd say this is the case.

The current URL, app/, didn't match any of these

I'm a newbie in Django and just started looking at it before a day by installing Django 1.10 on my local.
I've followed all the instructions of this link https://docs.djangoproject.com/en/dev/intro/tutorial01/. However I'm continuously getting this error:
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:
^admin/
The current URL, polls/, didn't match any of these.
I've created polls app to getting started.
To make a start, I went with view.py, here is the code:
polls/views.py
from django.shortcuts import render
# Create your views here.
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello World!")
Here is my code for urls.py:
polls/urls.py
from django.conf.urls import patterns, url
from . import views
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
)
And here is the urls.py in root:
mysite/urls.py
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)),
)
I've spent good piece of time to find out the solution, found that this question has been asked for many times,so tried with those solutions as well but none of them worked for me.
I can't find out what I'm missing here so please draw my attention to the gap.
I think you have edited the wrong file when trying to change the root url config.
Make sure you are editing the root url config in mysite/mysite/urls.py (the directory containing settings.py) not mysite/urls.py (the directory containing manage.py).
As general advice, install the latest release, currently 1.9. Don't use 1.10, which is under development. Make sure that you are following the tutorial for 1.9, because the tutorial changes for different versions. For example, your mysite/urls.py doesn't match the tutorial for 1.9, as the urlpatterns should be:
urlpatterns = [
url(r'^polls/', include('polls.urls')),
url(r'^admin/', admin.site.urls),
]
In settings.py you have a setting name INSTALLED_APPS-
Adds you app i.e. polls to it.
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
....
'polls',
]
It's working. Go to your url bar and type your app name:
http://127.0.0.1:8000/home
My app name is home. It will work.
If you want to set your app as your default page then import views from your app.
Like
from home import views
then write
url(r'^$', views.index),
inside.
It will set the views as your default page
http://127.0.0.1:8000/
When you type this it will redirect to your views.
I had the same problem as described. Running a Windows machine.
It turned out, that the virtual environment I was using had not been configured properly (or maybe I had forgot to activate it before installing Django) and the interpreter and django-admin were fetched from the wrong path by CMD.EXE.
If it appears as if Django is "ignoring" your urls.py - try deleting everything, re-creating the virtual environment, activating it and re-installing Django afterwards.
Make sure you have path("admin/", admin.site.urls) in your main url settings.
change the mysite/url
from django.conf.urls import patterns,include, url
from django.contrib import admin
urlpatterns = patterns('',
url(r'^&', include('polls.urls')),
url(r'^admin/', include(admin.site.urls)),
)
Then run your server and visit 127.0.0.1/8000.
This should take you to the index of your website.
or you leave your code as it is and run 127.0.0.1/8000/polls on your browser
if #Alasdair answer does not work, and it seems your working on correct files, just restart your server
The path in Django 2.2 does not support regular expression , I was using, path('(?P<id>\d+)/share/', views.mail_send_view) , so getting error, now changed to this, path('<int:id>/share/', views.mail_send_view). Now not getting any error. For more info please follow django's official documentation.
I too had same problem going through the official docs tutorial. I was using cloud 9. What I realised before I was able to solve this problem was that while creating the workspace I already chose django(ie by the time my workspace was created, django had already been installed) And going through the tutorial, I again executed $ django-admin startproject mysite thereby having another layer of django with multiple directories and unavoidably the confusion. My solution was to delete everything and start all over.
Checklist:
Make sure to run the server
>>python manage.py runserver
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
February 01, 2020 - 15:04:46
Django version 3.0.2, using settings 'corona.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
Open the browser with cmd running, http://127.0.0.1:8000/polls
should be added to the url.

Django Tutorial 3 - URL producing 404 error

I am following the Django 1.6 tutorial 3 (https://docs.djangoproject.com/en/1.6/intro/tutorial03/) and I have become stuck towards the beginning - "Writing your first view".
The Django tutorial first says to edit the views.py, which I have done as follows:
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, world. You're at the poll index.")
Then it says to create a urls.py in the same directory, which I have done. My urls.py looks like this:
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^polls/', include('polls.urls')),
url(r'^admin/', include(admin.site.urls)),
)
The next instruction is to now check the URL http://localhost:8000/polls/ for the message 'Hello, world. You're at the poll index.' but my browser simply produces the Django 404 error - Page not found.
I'm sure I'm doing something pretty stupid but I can't what is wrong, I've tried restarting the python webserver. I've followed the previous 2 tutorials with success, can anybody help me out?
If it makes a difference, I'm using Windows7, Python 3.4.1 and Django 1.6.5
Your view.py for polls must be in projectname/polls/ directory when first urls.py will be in projectname/projectname/.
Then, if you decide to configure and add more polls app specific urls, you can create urls.py in projectname/polls/.

Categories