The issue is caused by a circular import. Django - django

Hello colleagues! I was working in Django project. I have a problem with my urls
In the project I only have one app within that app, I created my urls file to later import it into the urls of the entire project, but when the server was running, it gave me the following error:
The included URLconf 'online_store.urls' 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.
My urls.py project
from django.contrib import admin
from django.urls import path, include
urlspatterns = [
path('admin/', admin.site.urls),
path('', include('online_store_app.urls')),
]
And my urls.py of app
# Dajngo
from django.urls import path
from online_store_app import views
urlspatterns = [
# urls site
path('home', views.home, name = 'home'),
path('services', views.services, name = 'services'),
path('store', views.store, name = 'store'),
path('blog', views.blog, name = 'blog'),
path('contact', views.contact, name = 'contact'),
]

The problem is just a typo, in both files you need to write urlpatterns not urlspatterns (there is no s between url and patterns).

Related

How to remove the unspecified space between root and include url in django 2.2

I am trying to create a Django website. I have specified the path to one of my apps. But I am getting an unspecified space in the pattern.I am using Django 2.2. screenshot of the django debug page
my base urls.py file
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('', include('homepage.urls')),
path('account/', include('account.urls')),
path('admin/', admin.site.urls),
path('db/', include('dashboard.urls')),
]
my dashboard/urls.py file
from django.urls import include, path
from .import views
urlpatterns = [
#author dashboard
path('authordboard/<int:auth_id/',views.author_dboard, name='author_details'),
#reviewer dashboard
path('reviewdboard/<int:re_id/',views.re_dboard, name='reviewer_details'),
]
I have included the dashboard app in the settings.py file.
Please help! Thanks in advance!
you must close variable declared in url. in your dashboard/urls.py:
from django.urls import include, path
from .import views
urlpatterns = [
#author dashboard
path('authordboard/<int:auth_id>/',views.author_dboard, name='author_details'),
#reviewer dashboard
path('reviewdboard/<int:re_id>/',views.re_dboard, name='reviewer_details'),
]

Routing error: trying to route a defined route in urls.py

I encountered a strange behavior of Django urls. Although forecast/upload url is defined in the urls.py, Django is saying
Page not found (404)
Request Method: POST
Request URL: http://localhost:8000/forecast/upload/
Using the URLconf defined in myproject.urls, Django tried these URL patterns, in this order:
polls/
admin/
forecast/ [name='index']
forecast/ **forecast/upload/** [name='upload']
forecast/ help.html [name='help']
The current path, **forecast/upload/**, didn't match any of these.
Project url.py:
from django.contrib import admin
from django.urls import include, path
from django.conf.urls import url
urlpatterns = [
path('polls/', include('polls.urls')),
path('admin/', admin.site.urls),
path('forecast/', include('forecast.urls')),
]
Application urls.py:
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
#url(r'^$', views.home, name="home"),
path('forecast/upload/', views.upload, name="upload"),
path('help.html', views.help, name="help"),
]
You have specified "forecast" twice; once at project level and once in the app. So your URL would be "forecast/forecast/upload".
Presumably, you don't want that, in which case you should remove the "forecast" from the pattern in the app urls.

no module named search after adding project url

I've had the pleasure to work with somebody yesterday on the issue with my urls here Adding an additional template to an existing project errors out, but after trying everything suggested i'm still in the same situation.
My project is named mysite and my application is search.
It was suggested to add the following to my project urls.py
url(r'^search/', include('search.urls')),
When doing so I'm given the error ModuleNotFoundError: No module named 'search'.
My project urls.py is the following:
from django.conf.urls import url, include
from django.contrib import admin
from django.views.generic import TemplateView
from django_filters.views import FilterView
from mysite.search.filters import UserFilter
urlpatterns = [
url(r'^$', TemplateView.as_view(template_name='home.html'), name='home'),
url(r'^search/$', FilterView.as_view(filterset_class=UserFilter, template_name='search/user_list.html'), name='search'),
url(r'^admin/', include(admin.site.urls)),
url(r'^search/', include('search.urls')),
]
I'm attempting to add the following to my app urls.py
from django.conf.urls import url, include
from django.contrib import admin
from django.views.generic import TemplateView
from django_filters.views import FilterView
from mysite.search.filters import UserFilter
from . import views
urlpatterns = [
url(r'^results/$', views.results, name='results'),
]
I have an empty view for results defined as
def results(request):
return render(request, 'results.html')
When I try to add the following POST to my form for the results it gives me the error in the first post. When I have the results url in my app.urls.py
<form action = "{% url 'results' %}" form method = "POST">
This is what my current application structure looks like. Please help get me on the right track. Thank you.
Your search directory is in your mysite directory (the one that includes settings.py. That means you should include mysite.search.urls (just as you use mysite.search in your import and INSTALLED_APPS).
from mysite.search.filters import UserFilter
urlpatterns = [
...
url(r'^search/', include('mysite.search.urls')),
]
If your search directory was in your project directory (the one that includes manage.py, then you would remove mysite from the import, include() and INSTALLED_APPS.
from search.filters import UserFilter
urlpatterns = [
...
url(r'^search/', include('search.urls')),
]

Adding a new url gives error

In my urls.py I have this.
from django.conf.urls import include, url
from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static
from events import views as eviews
urlpatterns = [
url(r'^user/', include('accounts.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'^$', eviews.events_list, name='events_list'),
url(r'^(?P<o_type>[-\w]+)/$',
eviews.events_list,
name='events_list_by_org'),
url(r'^(?P<id>\d+)/(?P<slug>[-\w]+)/$',
eviews.event_detail, name='event_detail'),
]
when I add the "user" url which is at first line in urlpatterns, it shows error on line 26 which is "events_list_by_org" url. When I comment out or remove 'user' url it works fine but shows error when it's included.
Try separating your event urls like your accounts url inside account app, and adding namespece to accounts and events.
urls.py in core
urlpatterns = [
url(r'^user/', include('accounts.urls', namespace='accounts')),
url(r'^event/', include('event.urls', namespace='event')),
]
urls.py in event app
urlpatterns = [
url(r'^$', eviews.events_list, name='events_list'),
url(r'^(?P<o_type>[-\w]+)/$',
eviews.events_list,
name='events_list_by_org'),
url(r'^(?P<id>\d+)/(?P<slug>[-\w]+)/$',
eviews.event_detail, name='event_detail'),
]
Maybe it's because of follwoing namespaces, so trying your namespace to user urls.

Django Rest Framework URL Mapping for multiple apps

I have a django-rest project called main and under it I have created an app called users. So, my project has the files :-
main/main/urls.py
and
main/users/urls.py
In users/urls.py I have
from django.conf.urls import url, include
from rest_framework import routers
from users import views
router = routers.DefaultRouter()
router.register(r'users', views.UserViewSet)
and in the main/main/urls.py I have
from django.conf.urls import url
from django.contrib import admin
from users import urls
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^users/', users.urls),
]
However, I keep getting the error NameError: name 'users' is not defined. What is the correct way to set up urls when I have multiple apps? I would like to have a urls.py file for each app that is independent of the project. And in the root urls.py would include routing to different apps.
You import url not user, can try it
from users import urls as users_url
# ^^^^^^^^
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^users/', users_url),
# ^^^^^^^
]
but better:
from django.conf.urls import url, include
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^users/', include('users.url')),
# ^^^^^^^
]
more details including-other-urlconfs