I am writing basic views and then putting it in urls.py but it is still not working.
views.py
from django.http import HttpResponse
def home(request):
return HttpResponse("hello how are you")
project urls.py
from django.contrib import admin
from django.urls import path,include
urlpatterns = [
path('admin/', admin.site.urls),
path('',include('blog.urls')),
]
blog/urls.py
from django.urls import path
from . import views
urlpatterns = [
path('home/',views.home,name='home')
]
I have also included my app in settings of project (Installed apps).
Some thoughts about that:
Change your urls patterns in your apps urls to:
from .views import *
urlpatterns= [
path('', home,name='home')
]
If its not working send us your installed apps settings and wsgi file
You confused, you need to write http://127.0.0.1:8000/home not only http://127.0.0.1:8000 with your current code.
If you want the view to be rendered in default route so change urls.py as:
from . import views
urlpatterns= [
path('', views.home,name='home')
]
Otherwise, you can only append /home with your current code to work.
for the same code above it is working for the URL http://127.0.0.1:8000/home/
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'),
]
these are my main codes i wrote to create wepapp but i get this 404 error so pleas help
my hello urls.py
from django.urls import path
from hello import views
urlpatterns = [
path("", views.index, name="index")
]
my urls.py
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('admin/', admin.site.urls),
path('hello/', include("hello.urls")),
]
my views.py
from django.http import HttpResponse
from django.shortcuts import render
# Create your views here.
def index(request):
return HttpResponse("hello, orld")
It looks like you are trying to access index with
http://127.0.0.1:8000/. This will not work, because your index is included in hello's urls.py, and hello's url starts with hello/.
Try
http://127.0.0.1:8000/hello
instead.
I'm very new to working with Django and I've been relying on some tutorials to link this one to React, but the problem is that initially (when I open 127.0.0.1:8000) React loads the routes perfectly, then when I reload the page Django tries to interpret the path from urls.py and obviously can't find it.
The error is:
Page not found (404) Using the URLconf defined in memberstack.urls, Django tried these URL patterns, in this order:
admin/
api/token-auth/
core/
I hope you can help me, thanks in advance
my_project/urls.py
from django.contrib import admin
from django.urls import path, include
from rest_framework_jwt.views import obtain_jwt_token
from frontend.views import index
urlpatterns = [
path('', include('frontend.urls')),
path('admin/', admin.site.urls),
path('api/token-auth/', obtain_jwt_token),
path('core/', include('core.urls')),
]
frontend/urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.index),
]
frontend/views.py
from django.shortcuts import render
def index(request):
return render(request, 'frontend/index.html')
For this, you'll have to use a catch-all in order for React to handle all the routing instead of django there.
from django.urls import path, re_path
from . import views
urlpatterns = [
path('', include('frontend.urls')),
path('admin/', admin.site.urls),
path('api/token-auth/', obtain_jwt_token),
path('core/', include('core.urls')),
re_path(r'^(?:.*)/?$', include('frontend.urls')),
]
Or
urlpatterns = [
path('', views.index),
re_path(r'^(?:.*)/?$', views.index)
]
I think the better practice would be to implement Django-Rest-Framework and build them separately.
I have searched endlessly for a solution and I can't seem to find it, even though I know this has been asked before. I am encountering a perpetual 404 error from the django tutorial. It appears to me that the 'urlpatterns' isn't recognizing another option of than admin.
backend/urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('test/', include('meat.urls')),
path('admin/', include(admin.site.urls)),
]
backend/meat/urls.py
from django.contrib import admin
from django.urls import path, include
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
backend/meat/views.py
from django.shortcuts import render
from django.http import HttpResponse
def index(requests):
return HttpResponse('Hello Wold')
In order to access views inside the meat app.
you have included the urls of meat to the test/ path.
Thus you should use url /test/
if you are developing on local host then localhost:8000/test/
NB: I have tested this right now link
PS: try changing the path of admin site to path('admin/', admin.site.urls),
after wasting saturdays eve googling for a solution to finally make a Django server work, i need your assistance..
I first of all want to set up my project in that way that http://127.0.0.1:8000/ redirects me to the index.html site. But somehow I am not able to run the Django server within my virtualenv (access denied).
I handled error over error in the past few hours (inserted a Secret key, inserted silenced_system_checks since E408/09/10 occured as errors before the current error) and here I stuck now. I am not capable to understand the prompt error at all. I assume that Django wants to start the server but can't find a file/html to return?
urls.py // dassocc_app dir
from django.urls import path
from django.urls import include
from django.conf.urls import url
from . import views
urlpatterns = [
path('/dasocc_site/dasocc_app/templates/', include("dasocc_app.views")),
path('', views.liga, name="index"),
]
views.py
import requests
from django.shortcuts import render
def liga(request):
liga = ['1. Bundesliga', 'Premier League', 'La liga']
return render(request, 'dasocc_app/templates/index.html', {'liga': liga})
urls.py // dasocc_site dir
from django.urls import path
from django.urls import include
from dassoc_app import views
urlpatterns = [
url(r'^$', views.liga, name='index')
]
enter image description here
Your troublemaker is the line path('', views.index, name="index"). It cannot find a function called index in your views.py.
Assuming the function you want to call is liga() you will have to write
path('', views.liga, name="index").
Or you could rename your liga function to index
#2
Kindly change your dassoc_site.urls:
dassoc_site/urls.py
from dasocc_app import views
from django.conf.urls import url, include
urlpatterns = [
url(r'^$', views.liga, name='index'),
url(r'^dassoc-app/', include('dassoc_app.urls')),
]
dassoc-app/urls.py
from django.conf.urls import url
from dasocc_app import views
urlpatterns = [
# Where home is some random view from your dassocc-app
url(r'^$', views.home, name='home')
]