Module Not FoundError Python Django - python

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

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.

Routing with Django and React

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.

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

Categories

Resources