How to do urlconf in django 2.0+ - python

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

Related

Django FirstApp

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

django circular import on urls

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

Keep getting 404 errors with django 2.2

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

getting name error when try to define urls in Django

I want to define URLs in Django but get an error:
from django.contrib import admin
from django.urls import path
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.index, name="index"),
]
the error is:
nameError(name'view' is not defind)
please inform me.
thanks,
Saeed
You need to import views. If you have an app named myapp and it has views.py inside the app directory, then import it like this:
from django.contrib import admin
from django.urls import path
from myapp import views # <--- Here
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.index, name="index"),
]

Module Not FoundError Python Django

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

Categories

Resources