I don't know why this problem happened.enter image description here
These errors occur when you read a book and follow it. I don't know what to do. Give me a hand.
Source Code
This is my fistsite.py source code.
fistsite.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
enter code here
path('admin/', admin.site.urls),
path('polls/', include('polls.urls')),
]
After changing the source code above
now fistsite.py
from django.contrib import admin
from django.urls import path, include
from . import views
urlpatterns = [
path('', views.index, name='index'),
path('/polls', views.polls, name='polls'),
path('/admin', views.admin, name='admin')
]
But there was an error.enter image description here
my projcetenter image description here
Assuming that you have Django version > 2.0, you should add to your urls.py file something like this:
from django.contrib import admin
from django.urls import path, include
from . import views
urlpatterns = [
path('', views.index, name='index'),
path('/polls', views.polls, name='polls'),
path('/admin', views.admin, name='admin')
]
So as shown in the error page, your Django project tried to go to localhost:8000/admin/ and localhost:8000/polls/ but the site you are trying to access in localhost:8000/ . You haven't defined any url pattern for capturing this scenario. What you can do is in your urls.py, add this line under urlpatterns :
urlpatterns=[
path('', views.index, name='index'),
...............
]
This works if you are using Django version>2.0
If you are using Django version<2.0 then you can add this:
urlpatterns=[
url(r'^$', views.index, name='index'),
...............
]
If you want to access you "polls". After change your fistsite.pytry this
url = "localhost:8000/polls/
and you are giving only localhost:8000 if did not create any app then this url will work.
Related
I am building an app using django and notice that some new urls that i have created, the list is provided below are not being found.
I read somewhere that the slug might be the reason django cannot found the urls but i am not sure
main urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path(config("ADMIN_URL"), admin.site.urls),
path("", include("core.urls", namespace="core")),
]
application urls.py
from django.contrib import admin
from django.urls import path, include
from . import views
app_name = 'core'
urlpatterns = [
path('index/', views.index, name='index'),
path('home/', views.home, name='home'),
path('home/<slug:slug>/', views.user_lessons_details, name='user-lessons-details'),
path('home/join-transaction/', views.user_lessons_join_transaction, name='user-lessons-join-transaction'),
path('home/start-transaction/', views.new_test_epoint, name='new-test-epoint'),
path('home/draft/<slug:slug>/', views.user_lessons_core_action, name='user-lessons-draft'),
path('teachers/lessons/create/', views.merchant_lessons_create, name='teachers-lessons-create'),
path('teachers/lessons/list/', views.teachers_lessons_list, name='teachers-lessons-list'),
path('teachers/lessons/<slug:slug>/', views.teachers_lessons_details, name='teachers-lessons-details'),
path('teachers/lessons/<slug:slug>/update/', views.teachers_lessons_update, name='teachers-lessons-update'),
path('teachers/lessons/<slug:slug>/delete/', views.teachers_lessons_delete, name='teachers-lessons-delete'),
path('teachers/drafts/create/', views.teachers_core_actions_create, name='teachers-drafts-create'),
path('teachers/drafts/list/', views.teachers_core_actions_list, name='teachers-drafts-list'),
path('teachers/drafts/<slug:slug>/', views.teachers_core_actions_details, name='teachers-drafts-details'),
path('teachers/drafts/<slug:slug>/update/', views.teachers_core_actions_update, name='teachers-drafts-update'),
path('teachers/drafts/<slug:slug>/delete/', views.teachers_core_actions_delete, name='teachers-drafts-delete'),
path('teachers/home/list/', views.teachers_home_list, name='teachers-home-list'),
path('teachers/home/<slug:slug>/', views.teachers_home_details, name='teachers-home-details'),
path('teachers/home/create/course/', views.teachers_home_transaction_quote, name='teachers-home-create-course'),
path('teachers/home/create/something/', views.teachers_home_create_transaction, name='teachers-home-create-something'),
path('teachers/home/create/fields-selection/', views.teachers_home_fields_selection, name='teachers-home-fields-selection'),
# ALL THE NEW URLS BELOW return NOT FOUND in django no matter what i do
path('teachers/home/account/settings/', views.teachers_settings, name='teachers-settings'),
path('teachers/account/settings/update-password/', views.teachers_settings_update_password, name='teachers-settings-update-password'),
path('teachers/account/payments/', views.teachers_payments, name='teachers-payments'),
path('teachers/account/payments/update/', views.teachers_payments_update, name='teachers-payments-update'),
path('teachers/account/analytics', views.teachers_analytics_stuff, name='teachers-analytics-stuff')
]
How can i possibly solve this urls not found issue in django?
Please dont mind the urls meaning
I just started learning Django and got the following error:
[![The site doesn't see URL and then Error 404][1]][1]
urls.py - site
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('webexample/', include ('webexample.urls')),
]
urls.py - webexample
from django.urls import path
from . import views
urlpatterns = [
path(r'^$', views.index, name='index'),
]
views.py - webexample
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return HttpResponse("<h3>This page isn't set up yet...</h3>")
Error photo
change path to this to work for the current url.
path(r'', views.index, name='index')
You could do this
path('index/', views.index, name='index'),
or
re_path(r'^index/$', views.index, name='index'),
to know more about this, you can the django doc https://docs.djangoproject.com/en/3.0/ref/urls/
I'm very new to Django, while going through the tutorials how to start different apps inside of a project is clear to me, but not setting up the index page of the entire website.
Inside of my project folder it looks something like:
Projectname
App1
App2
App3
What I want to do is create a url with path looking something like this
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('', include('index.urls')), #Homepage url
path('App1/', include('App1.urls')),
path('accounts/', include('django.contrib.auth.urls')),
path('App2/', include('App2.urls')),
path('App3/', include('App3.urls')),
path('admin/', admin.site.urls),
]
So that you can visit http://127.0.0.1:8000/ (or website.com) and see an index page with a corresponding view that I can use to show content from a database.
Maybe its as simple as creating an index-app and setting the path to (''), but I haven't been able to accomplish that.
You can just create index view inside app (e.g. app1), and use this view direcly with urlpatterns, like this:
from django.contrib import admin
from django.urls import path, include
from app1 import views
urlpatterns = [
path('', views.index), #Homepage url
path('App1/', include('App1.urls')),
path('accounts/', include('django.contrib.auth.urls')),
path('App2/', include('App2.urls')),
path('App3/', include('App3.urls')),
path('admin/', admin.site.urls),
]
Or if you'd like to keep index in undex.urls, you should add to index.urls file empty path:
# index urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
# project urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('', include('index.urls')), #Homepage url
path('App1/', include('App1.urls')),
path('accounts/', include('django.contrib.auth.urls')),
path('App2/', include('App2.urls')),
path('App3/', include('App3.urls')),
path('admin/', admin.site.urls),
]
Okay... let's try to explain things clearly. I've used Python Django to create a dynamic webpage/web-app. After completing the website I have published it using DigitalOcean and have successfully attached my purchased domain name to the name server of DigitalOcean. When I access my website, ordinanceservices.com, i get an error 404; however, if I type ordinanceservices.com/home it works as it should and displays the home page. How, by editing the python files, can I have it to where ordinanceservices.com will display the home page as opposed to error 404? I feel like there's something that I am doing that is fundamentally wrong regarding my .urls structure and thus a rewrite/redirect in the nginx config should not be necessary.
Here is the specific error:
Page not found (404)
Request Method: GET
Request URL: http://ordinanceservices.com/
Using the URLconf defined in django_project.urls, Django tried these URL patterns, in this order:
^admin/
^ ^home/ [name='home']
^ ^contact/ [name='contact']
^ ^services/ [name='services']
The current URL, , didn't match any of these.
I somewhat understand what is happening here though I do not know how to fix this. Next I will provide my .urls files for each folder that contains such:
/django_project urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^', include('company.urls')),
)
/company urls.py
from django.conf.urls import url, include
from . import views
urlpatterns = [
url(r'^home/', views.home, name='home'),
url(r'^contact/', views.contact, name='contact'),
url(r'^services/', views.services, name='services'),
]
/company views.py
from django.shortcuts import render
def home(request):
return render(request, 'company/home.html')
def contact(request):
return render(request, 'company/contact.html')
def services(request):
return render(request, 'company/services.html')
What I am aiming to do, without needing to redirect the main URL using the nginx config files to do so, is to edit my urls and views structure of my Python files to ensure that the normal URL, ordinanceservices.com, will actually display a page; preferably the home page of my webpage.
I have a hunch that it has to do with the fact that I do not have a views.index for the r'^admin/' to reach to. I am not certain but I have been trying to figure this out for hours. Does anyone have a clue what I can do to fix this?
You haven't defined anything at the root url. Add one more line to your company urls.py so it becomes
//company urls.py
from django.conf.urls import url, include
from . import views
urlpatterns = [
url(r'^$', views.home, name='home'),
url(r'^home/', views.home, name='home'),
url(r'^contact/', views.contact, name='contact'),
url(r'^services/', views.services, name='services'),
]
First of all, check if you have added www.yourdomain.com or yourdomain.com to ALLOWED_HOST = ['www.yourdomain.com','yourdomain.com'] in your settings.py
and then in your company/urls.py do this
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.home, name='home'),
url(r'^contact/$', views.contact, name='contact'),
url(r'^services/$', views.services, name='services'),
]
and in your main urls.py add this code
from django.conf.urls import url,include
from django.contrib import admin
admin.autodiscover()
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^', include('company.urls')),
]
I'm trying out Python Django. With eclipse pyDev. But I'm unable to simply get my first url to display.
This urls.py is from the Cr package.
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index, name='index')
]
This urlspy is from the Crowd package.
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'Crowd/', include('Cr.urls'))
]
So what I've understood from this the Crowd package is the "main" webservice(?), and by using include I can whenever the regular expression matches Crowd, will pass it on to the other urls.py(Cr). But the debugger passes:
Using the URLconf defined in Crowd.urls, Django tried these URL patterns, in this order:
^admin/
The current URL, crowd, didn't match any of these.
my views.py file
from django.shortcuts import HttpResponse
def index(request):
return HttpResponse('<h1>Hello World!</h1>')
I tried to access it with http://127.0.0.1:8000/Crowd
Below is an image of the project folder.
Can we see your settings.py file? There is a place in there where you define your project's url file. I'm assuming it's right now either not there or it's pointing to the wrong place, because Django can't find your urls.py file.
For example, in my settings.py file for one of my projects, I have:
ROOT_URLCONF = 'Freya.urls'
Where "Freya" is my project name
Just for reference, not that I know this will solve your problem, this is what (part of) my urls.py file looks like for one of my projects:
from django.conf.urls import patterns, url
import views
urlpatterns = patterns(
'',
url(r'^$', views.index, name='index'),
url(r'^login/$', views.login, name='login'),
url(r'^logout/$', views.logout, name='logout'),
)
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'Crowd/', include('Cr.urls'))
]
just use this urls.py file