getting name error when try to define urls in Django - python

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

Related

url issues with django

I can't get my urls to work for django no matter what I try
from django.contrib import admin
from django.urls import include, re_path
from django.conf.urls import url
from catalog import views
urlpatterns = [
re_path(r'^admin/', admin.site.urls),
re_path(r'^fruit/?$', views.fruit_catalogue, name='fruit_catalogue'),
re_path(r'^fruit/?$', views.fruit_details),
re_path(r'^fruit/(?P<fruit_id>[1-7]{1})/?$', views.fruit_details),
]
There are 2 url.py files that you should check for the correct functioning of the URLs.
First the one, that is in your project, that should look something like this:
from django.contrib import admin
from django.urls import include, path
from django.conf import settings
urlpatterns = [
path('admin/', admin.site.urls),
path("", include("NAMEOFAPP.urls")),
]
and the other is inside your app. and a simple version of it should look something like this:
from django.urls import path
from . import views
urlpatterns = [
path("home", views.index, name="index"),
path("login", views.login_view, name="login"),
path("register", views.register, name="register")
]
Try to use this same setup and you should be perfectly fine.

Include method under urls.py not working: Django

from django.contrib import admin
from django.urls import path, include
from pages import views
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.index, name='index'),
path('dashboard/', include(pages.urls)),
]
I'm trying to use include method under my urls.py file to direct the dashboad/ request to my app urls.py file, which is not working.
change this line :
path('dashboard/', include(pages.urls)),
to this :
path('dashboard/', include('pages.urls')),
see this link
Hi and welcome to StackOverflow!
The problem you are facing is that Django expects the include(...) method to be given a list of url patterns or the string of the module url's you want to be included at the path.
I would refactor your code like so:
from django.contrib import admin
from django.urls import path, include
from pages import views
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.index, name='index'),
path('dashboard/', include('pages.urls')),
]

URL http://localhost:8000/admin/ redirects to wrong page

I'm trying to reach the Django admin page but when I type in http://localhost:8000/admin/ it pops up with my web application instead.
For more context I have two url.py's in separate folders.
from django.contrib import admin
from django.urls import path
from . import views
urlpatterns = [
path('', views.project_list, name='list'),
path('<slug:project_slug>/', views.project_detail, name='detail')
]
and...
from django.urls import path, include
urlpatterns = [
path('', include('budget.urls')),
path('admin/', admin.site.urls),
I'm not sure why http://localhost:8000/admin/ doesn't go to Django's admin page but if anyone has any suggestions that would be very much appreciated.
In your project level urls.py place admin url in top.
Like this
urlpatterns = [
path('admin/',admin.site.urls),
path('',include('budget.urls'))
]
Change urlpatterns order here:
from django.urls import path, include
urlpatterns = [
path('', include('budget.urls')),
path('admin/', admin.site.urls),
like this:
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('budget.urls')),
In your code pattern path('<slug:project_slug>/', views.project_detail, name='detail') resolve before path('admin',admin.site.urls),.
See Django documentation

Django URL Does not Exist?

This is very strange, any ideas?:
Here is my code for urls.py
from django.contrib import admin
from django.conf.urls import include
from django.urls import path
urlpatterns = [
path('admin/', admin.site.urls),
path('videos/', include('videos.urls'))
]
And videos/urls.py:
from django.urls import path
from . import views
urlpatterns = [
path(r'^$', views.index, name='index'),
]
Actually, I fixed it! Simple mistake. Change videos\urls.py to this;

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