Django not using updated urls.py - returning 404 on www.site.com/page with outdated list - django

I am very new to django and beginning to understand some of the framework however view-route binding is confusing me
There is a persistent issue that when I try to visit any url except for the homepage and /admin I receive a 404, including routes I have declared in my project's urls.py file
also i am following this mdn tutorial
project urls.py
"""trends URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/2.2/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
from django.views.generic import RedirectView
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
path('articles/', include('articles.urls')),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
app named 'articles' urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
app named 'articles' views.py
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, world. You're at the articles index.")
and here is the 404 page I receive
I know this is becoming very long but there is one more odd thing, when I refresh the 404 page, it will toggle between showing me the above screenshot and sometimes show me an old route which is no longer in the urls.py like this
this is on an nginx server with gunicorn, and restarting the nginx service does not solve the issue

In your projects urls.py you have defined
path('articles/', include('articles.urls')),
So by going to YOUR_URL/articles will not give a valid response. Instead try going to YOUR_URL/articles/ or change your path to
path('articles', include('articles.urls')),

Stumbled upon this SO post which lead me to the idea to restart gunicorn and that solved my problem so try running
sudo service gunicorn restart
should fix your problems

Related

Django Ignoring updates to urls.py

The urls.py file In My Dango Project Is being Ignored; for Example I Tried Commenting Out The Admin Urls From the Project, And I Could Still Use The Admin. I tried Adding A Name Space, And Django Says It Doesn't Exist.
mysite/urls.py:
"""mysite URL Configuration
The `urlpatterns` list routes URLs to views. For more information, please see:
https://docs.djangoproject.com/en/3.0/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
from django.conf.urls.static import static
from . import settings
urlpatterns = [
path('admin/', admin.site.urls),
path('',include("Wallet.urls"),
path('',include("django.contrib.auth.urls"))
] + static(settings.STATIC_URL, document_root='/home/dh_hemuuv/minicoin.fztl.com/static')
Wallet/urls.py:
from django.urls import path
from .views import signup,viewWallet,transfer,userpage,collectgift
urlpatterns = [
path("signup/",signup,name="signup"),
path("",viewWallet,name="home"),
path("transfer/",transfer,name="transfer"),
path("account/<str:username>/",userpage),
path("collectgift/<int:id>/",collectgift,name="colgift"),
]
Please Help!
oops I Found My Problem I found a Pesky __pycache__ Folder Deleteing Solved my problems

how to correctly import urls.py from app?

This is probably pretty simple but I can't get my head around it. I'm learning Django, have v3.0.4 installed and can't get the URLs from an app to work correctly.
On the project urls.py I have the following:
Project\urls.py:
from django.contrib import admin
from django.urls import path
from django.urls import include
from AppTwo import views
urlpatterns = [
path('', views.index, name='index'),
path('', include('AppTwo.urls')),
path('admin/', admin.site.urls),
]
I've created an app named "AppTwo" and have the following urls.py and views.py in the app:
AppTwo\urls.py:
from django.urls import path
from . import views
urlpatterns = [
path('/help', views.help, name='help'),
]
AppTwo\views.py:
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def index(request):
return HttpResponse("<em>My Second App</em>")
def help(request):
return HttpResponse("<em>Help Page!!!</em>")
If I browse to http://127.0.0.1:8000/ the index page loads and I see the text "My Second App" as expected. However if I browse to http://127.0.0.1:8000/help I get page not found 404 error.
I can also browse to the admin page just fine. So far this is a stock project, the only other change I made after creating it was to the settings.py file to install the "AppTwo" application. Based on the documentation, this looks like it should work, so what am I doing wrong?
yep, knew it was simple.
Changed
path('/help', views.help, name='help'),
to:
path('help/', views.help, name='help'),
all good now.

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),
]

urls.py django 2.0.2 need parameters

i'm learning django and i'm making a blog tutorial but it's from older version that i have, i have version 2.0.2 and i'm not understand documentation my problem is that i dont know how configure my urls.py
this is my three proyect:
i need to put archive.html in 127.0.0.1:8000/ and this is my urls code
codigofacilito/blog.urls.py :
"""codigofacilito URL Configuration
The urlpatterns list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/2.0/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<br/>
from django.urls import path<br/>
from . import views<br/>
urlpatterns = [
path('admin/', admin.site.urls),
]
and codigofacilito/codigofacilito.urls.py:
from django.contrib import admin
from django.urls import path
from . import views
enter code here`urlpatterns = [
path('admin/', admin.site.urls),
]
from django.views.generic import TemplateView
urlpatterns = [
path('', TemplateView.as_view(template_name = 'archive.html')),
path('admin/', admin.site.urls),
]
put this in your project's urls.py, this will directly render the archive.html when you visit 127.0.0.1:8000/ in your browser.
but if you want to render data from backend in this html page then i suggest you to use views (function based , class based, etc..)
then you just have to import the views in url file and specify path for that.
from your_app.views import your_view
urlpatterns = [
path('/', your_view),
]

Django Project App Urls Page Not Found (404)

I still new to Django and I'm following this tutorial in YouTube. I can't proceed to the next tutorial because I hit an error.
When I type http://127.0.0.1:8000/music. I get this error.
Check my code below:
mysite urls.py
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^music/', include('music.urls')),
]
music urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index, name='index'),
]
music views.py
from django.http import HttpResponse
def index(request):
return HttpResponse("<h1>Hello World</h1>")
Please help.
You might have not saved the project/urls.py file.
Or to check whether the music is set in INSTALLED_APPS in settings.py?