I have a simple django project
structure is
examp-
exam
polls
templates
exam was the name of the project and polls is the name of the first app
I have the following code in exam/urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('polls/',include('polls.urls')),
]
and the following code in polls/urls.py
from django.urls import path
from views.polls import index
urlpatterns = [
path('', views.index, name='index'),
]
and i am getting the following error when i try to runserver
The included URLconf module 'polls.urls from
'/home/grr/Documents/examp/polls/urls.py'>' does not appear to have
any patterns in it. If you see valid patterns in the file then the
issue is probably caused by a circular import.
in polls/urls.py
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
And put index function in polls/views.py
with considering above answer code , and changing the first line using nameapp instead of "." without any error i could run my code successfully
from nameapp import views
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'),
]
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')
]
I am following this tutorial https://docs.djangoproject.com/en/2.2/intro/tutorial01/
and I am getting 404 errors when I try the first test saying it cannot find "polls/" below is some of my code
in mysite/urls.py
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('polls/', include('polls.urls')),
path('admin/', admin.site.urls),
]
in polls/urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
I tried searching but I can't find anything for django 2.2
Below is a screenshot of the error message
So the problem was my directory structure. it was mysite/urls.py when it should've been mysite/mysite/urls.py in the directory with settings.py not in the directory with manage.py
I have created a view at example.views.py, and here is my urls.py file:
from django.urls import path
urlpatterns = [
path('/', example.views.index, name='index'),
]
However, when I run django's runserver, I get the following error:
path('/', example.views.index, name='index'),
NameError: name 'example' is not defined
What do I need to do to fix this? Quoting the view doesn't help either.
Try changing it to:
from django.urls import path
from example import views
urlpatterns = [
path('/', views.index, name='index'),
]
I am trying to map the base URL to my "Learning Logs's" home page. here is the following code I have in my main urls.py file:
from django.contrib import admin
from django.urls import path
from django.urls import include
urlpatterns = [
path('admin/', admin.site.urls),
path(r' ', include('learning_logs.urls', namespace='learning_logs')),
]
I save the file and look at the terminal to see if there are any issues and it spits out the following error: ModuleNotFoundError: No module named 'learning_logs.urls'
I am no to python/django and following a book called the python crash course. not sure what I am doing wrong please help!
Best solution is check author's updates:
http://ehmatthes.github.io/pcc/chapter_18/README.html#updates
updates for urls.py
from django.urls import path, include
from django.contrib import admin
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('learning_logs.urls')),
]
Updates for learning_logs/urls.py
"""Defines url patterns for learning_logs."""
from django.urls import path
from . import views
app_name = 'learning_logs'
urlpatterns = [
# Home page.
path('', views.index, name='index'),
]