The problem appeared after I added a template to a minimal webapp. The template extends django-oscar's layout.html. Nothing else in the project extends layout.html.
My goal is simply to be able to use django-oscar templates to form the basis of web pages in my webapp. For some reason I am having no end of issues. This is only the latest error message. I have been struggling with this for days! When I resolve one issue, another shows up.
I made a minimal git repo for this problem: https://github.com/mslinn/django_oscar_problem
The repo has a requirements.txt file in case anyone wants to install the PIP modules necessary run the program.
I tried to ensure that I had the simplest possible project that shows the problem. In README.md I show the complete error message displayed in the web browser.
# /templates/welcome.html
{% extends 'oscar/layout.html' %}
... etc ...
# main/urls.py
from django.urls import include, path
from django.contrib import admin
from django.urls import path
from . import views
urlpatterns = [
path('', views.home, 'home'),
path('admin/', admin.site.urls, 'admin'),
path('hello/', views.hello, 'hello'),
path('', include(apps.get_app_config('oscar').urls[0])),
]
# main/views.py
from django.shortcuts import render
def home(request):
return render(request, "welcome.html", {})
I then ran the webapp with:
$ ./manage.py runserver
The error message in the web browser after visiting http://localhost:8000 is:
ValueError at /
dictionary update sequence element #0 has length 1; 2 is required
Request Method: GET
Request URL: http://localhost:8000/
Django Version: 3.1.6
Exception Type: ValueError
Exception Value:
dictionary update sequence element #0 has length 1; 2 is required
The problem is here:
urlpatterns = [
path('', views.home, 'home'),
path('admin/', admin.site.urls, 'admin'),
path('hello/', views.hello, 'hello'),
path('', include(apps.get_app_config('oscar').urls[0])),
]
The arguments you're passing to path() are not quite right. The signature for path is:
path(route, view, kwargs=None, name=None)
i.e., the third positional argument is expected to be kwargs passed to the view function - but you've passed a string instead, which is what causes the somewhat obscure error. I think you intend this to be the name, in which case you need to supply that as a named argument, i.e.,:
urlpatterns = [
path('', views.home, name='home'),
path('admin/', admin.site.urls, name='admin'),
path('hello/', views.hello, name='hello'),
path('', include(apps.get_app_config('oscar').urls[0])),
]
Note that this then results in a new TemplateDoesNotExist error in your sample repo - this is because the location of the templates directory is not somewhere that Django knows to look. You can fix this by adding the path to that directory to the DIRS configuration in your template settings, which is currently set to [].
Related
I'm a very beginner.
When I tried to go visit this (http://127.0.0.1:8000/admin), I couldn't. Here have shown page not found. What can be the solution?
Problem that I faced:
Page not found (404)
“D:\1_WebDevelopment\Business_Website\admin” does not exist
Request Method: GET
Request URL: http://127.0.0.1:8000/admin
Raised by: django.views.static.serve
Using the URLconf defined in business_website.urls, Django tried these URL patterns, in this order:
admin/
admin/
[name='index']
singup [name='handle_singUp']
login [name='handle_login']
logout [name='handle_logout']
contact [name='handle_contact']
frontend_orders [name='frontend_orders']
hire_me [name='hire_me']
^(?P<path>.*)$
The current path, admin, matched the last one.
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.
Problem : open the picture
business_website urls.py:
from django.contrib import admin
from django.urls import path,include
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('business_app.urls')),
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
business_website url.py : open the picture
business_app urls.py:
from os import name
from django.contrib import admin
from django.urls import path
from .import views
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.index, name="index"),
path('singup', views.handle_singUp, name= "handle_singUp"),
path('login', views.handle_login, name="handle_login"),
path('logout', views.handle_logout, name="handle_logout"),
path('contact', views.handle_contact, name="handle_contact"),
path('frontend_orders', views.frontend_orders, name="frontend_orders"),
path('hire_me', views.hire_me, name="hire_me")
]
business_app url.py : open the picture
Delete from business_app urls.py this row:
path('admin/', admin.site.urls),
You should not call it twice.
You should set MEDIA_URL in settings to something. Like:
MEDIA_URL = '/media/'
Django urls need to have a trailing slash and the url you tried to access does not have it, check in your settings.py file if APPEND_SLASH is set to false
Among Django's many built-in features is APPEND_SLASH, which by default is set to True and automatically appends a slash / to URLs that would otherwise 404.
You can turn off this option by just setting APPEND_SLASH = False
You can read more here about why django uses trailing slashes
You have the same admin path defined in your root urls.py and in your app. It should probably be in just the root. Remove it from:
from os import name
from django.contrib import admin
from django.urls import path
from .import views
urlpatterns = [
# path('admin/', admin.site.urls), ##### REMOVE
path('', views.index, name="index"),
path('singup', views.handle_singUp, name= "handle_singUp"),
path('login', views.handle_login, name="handle_login"),
path('logout', views.handle_logout, name="handle_logout"),
path('contact', views.handle_contact, name="handle_contact"),
path('frontend_orders', views.frontend_orders, name="frontend_orders"),
path('hire_me', views.hire_me, name="hire_me")
]
Edit
After trying, and failing to reproduce the OP's error on my machine using all the answers given as of this writing, it turned out my original answer was not correct (not incorrect, but also not the solution), in fact the original answer given by Jaime Ortiz, was most likely the correct one.
But why was it so hard to come to this realization? Within the link he provided was this, which is why, when I initially tried his solution it did not work. Below is from the answer provided by
All Іѕ Vаиітy in that link. Note that within the [] is my insertion.
Since django observes both the urls [the one with the trailing slash
and the one without] as different, if you are caching your app, Django
will keep two copies for same page at ...
So either use admin/ instead of admin, or apply APPEND_SLASH = False to your settings.py, clear your browser cache and then you can use either, Django will append the slash automatically.
I am making a blog app using Django. In my blog app, i have 6 paths. This is my app urls.py
from . import views
from .views import AddPostView
from django.urls import path
from .feeds import LatestPostsFeed
urlpatterns = [
path('', views.PostList.as_view(), name='home'),
# path('<slug:slug>/', views.PostDetail.as_view(), name='post_detail'),
path('upload/', views.image_upload_view, name="upload"),
path('add_post/', AddPostView.as_view(), name='add_post'),
path('<slug:slug>/', views.post_detail, name='post_detail'),
path("feed/rss", LatestPostsFeed(), name="post_feed"),
]
The app is working perfectly, but whenever i try to move the path('<slug:slug>/', views.post_detail, name='post_detail') up from the add_post url (code below) and try to open add_post
from . import views
from .views import AddPostView
from django.urls import path
from .feeds import LatestPostsFeed
urlpatterns = [
path('', views.PostList.as_view(), name='home'),
# path('<slug:slug>/', views.PostDetail.as_view(), name='post_detail'),
path('upload/', views.image_upload_view, name="upload"),
path('<slug:slug>/', views.post_detail, name='post_detail'),
path('add_post/', AddPostView.as_view(), name='add_post'),
path("feed/rss", LatestPostsFeed(), name="post_feed"),
]
suddenly i have an error saying :
Page not found (404)
No Post matches the given query.
Request Method: GET Request URL: http://localhost:8000/add_post/
Raised by: blog.views.post_detail
Using the URLconf defined in mysite.urls, Django tried these URL
patterns, in this order:
admin/
[name='home']
upload/ [name='upload']
<slug:slug>/ [name='post_detail']
The current path, add_post/, matched the last one.
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.
I don't understand why it happens and i am curious and want to know what has just happened. maybe there are some concepts about django urls that i don't know. Can you explain to me what happen in this error?
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).
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('polls/', include('polls.urls')),
]
There is an error when i add url to url.py.
when i run the code in terminal : 'python manage.py runserver' ; then the follwing error is displayed in the terminal -
django.core.exceptions.ImproperlyConfigured: The included URLconf
'<module 'polls.urls' from
'C:\\Users\\Administrator\\PycharmProjects\\website2\\mysite\\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.
I searched everywhere for the solution but i couldn't find it. Please help me to get out of it.
I think this error most of u misunderstand, circular error imports are very rare in django only in flask it is frequent .
This error is caused by mispelling 'urlpatterns' in the urls.py which has been created in a django app.
Also it can be caused by not defining it in your urlpatterns in the urls.py file
Probably the error is caused by one of the above.
Its that easy
make sure in your polls app, you have a file called urls.py, which should look something like this:
from django.urls import path
from . import views
urlpatterns = [
path('', views.your_view),
]
If you haven't configured the views.py page in your polls app, then you can leave urlpatterns blank for now and you shouldn't see any errors.
This error might be coming because of the adimn in urls.py
urlpatterns = [
path('admin/', admin.site.urls),
path('polls/', include('polls.urls')),
]
When the polls path was removed by me, the website was running properly. So try comment the polls path in urls
urlpatterns = [
path('admin/', admin.site.urls),
#path('polls/', include('polls.urls')),
]
I'm following the tutorial on Django's site to create a simple poll app. However, Django is unable to resolve "//127.0.0.1:8000/polls" , even though I've defined the regex in mySite/urls.py. I'm doing this in a virtualenv, with the latest Django (1.7) installed.
mySite/urls.py:
from django.conf.urls import patterns, include, url
from django.contrib import admin
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^polls/', include('polls.urls')),
)
mySite/polls/urls.py:
from django.conf.urls import patterns, url
from polls import views
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
)
mySite/polls/views.py:
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, world. You're at the polls index.")
mySite/settings.py:
...
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'polls',
)
....
ROOT_URLCONF = 'mySite.urls'
The error I'm getting:
Using the URLconf defined in mySite.urls, Django tried these URL patterns, in this order: ^admin/
The current URL, polls, didn't match any of these.
I had the same problem.
It turns out I was confused because of the multiple directories named "mysite".
I wrongly created a urls.py file in the root "mysite" directory (which contains "manage.py"), then pasted in the code from the website.
To correct it I deleted this file, went into the mysite/mysite directory (which contains "settings.py"), modified the existing "urls.py" file, and replaced the code with the tutorial code.
In a nutshell, make sure your urls.py file is in the right directory.
Django unable to resolve 127.0.0.1:8000/polls because url config defined as r'^polls/'.
Usual workaround:
mySite/urls.py:
from django.conf.urls import patterns, include, url
from django.contrib import admin
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^polls/', include('polls.urls')),
)
Note:
Whenever Django encounters include(), It chops off whatever part of the URL matched up to that point and sends the remaining string to the included URLconf for further processing.
mySite/polls/urls.py:
from django.conf.urls import patterns, url
from polls import views
urlpatterns = patterns('polls.views',
url(r'^$', 'index', name='index'),
)
Note: Instead of typing that out for each entry in urlpatterns, you can use the first argument to the patterns() function to specify a prefix to apply to each view function.
Answer If
If you want to access 127.0.0.1:8000/polls Note: without trailing slash
use view based url
url(r'^polls', 'polls.views.index', name='index'),
So now you can access 127.0.0.1:8000/polls without trailing slash.
You're accessing to http://yourdomain.com/, and you don't have any URL defined for "/".
You have two options:
If you want to access to the index page of your polls application you have to enter the URL: yourdomain.com/polls
You can also modify you mySite/urls.py file to access from just yourdomain.com
from django.conf.urls import patterns, include, url
from django.contrib import admin
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^$', include('polls.urls')),
)
To make the answer clear for beginners who has this issue by following the tutorial, the project root URLconf is the one in the same folder as settings.py which is:
mysite/mysite/urls.py
Just make sure import 'include'. The code looks like:
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^polls/', include('polls.urls')),
]
So in
mysite/mysite/settings.py:
The line should be:
ROOT_URLCONF = 'mysite.urls'
You don't need create a fresh new root URLconf.
Depending on where you put your ROOT urls.py, you set your ROOT_URLCONFIG accordingly, if you have it in your outermost folder containing manage.py then "urls" is ok. if you have it in someother folder then you have to do ".urls"
Credit for the answer to jerryh91
For more info about how it works, check How Django processes a request
You put the urls.py folder into the outer MySite folder, you are suppose to put it in the inner one so its not mySite/urls.py, but mySite/mySite/urls.py:
ran into the same mistake when i did the tutorial
Another way to access 127.0.0.1:8000/polls would be to redirect the browser when accessing 127.0.0.1:8000. It is done by editing .../mysite/mysite/urls.py as follows:
from django.conf.urls import include, url
from django.contrib import admin
from polls import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^polls/', include('polls.urls', namespace='polls')),
url(r'^$', views.index, name='index'),
]
Page not found?
If you get an error page here, check that you’re going to http://localhost:8000/polls/ and not http://localhost:8000/.
Source : https://docs.djangoproject.com/en/3.0/intro/tutorial01/
Actually the problem is that you didn't notice that
mysite/urls.py and polls/urls.py are two different files and you modified polls/urls.py instead of putting mysite/urls.py in the urls.py file in ...mysite\mysite folder.
In my case, it was a stupid mistake. I wanted to integrate the plugin django-tinymce, and test it. So following this guide, I did the step 3 and exported the variable to the path. As the server runned again, I received the not found error, showing the message:
Using the URLconf defined in testtinymce.urls, Django tried these URL
patterns, in this order: ....
But I didn't know what exactly it was, until I remembered exporting the variable DJANGO_SETTINGS_MODULE
running unset DJANGO_SETTINGS_MODULE in terminal solved my issue. Hope that it helps someone too.
Add the below line in your Mysite/urls.py
url(r'^$', views.index, name='index'),
and check. If you have created your project correctly, it should work. Else something like above might have happened to have more than one files so confused.
2017-10-05_12:03 ~/mysite/mysite
$ vi urls.py
2017-10-05_12:04 ~/mysite/mysite
$ cd ../..
2017-10-05_12:04 ~
$ mv mysite SENSIBLE_NAME_DJANGO_ROOT
i had the same issue and got it resolved by adding /polls after http://server:port/ and so final address in server looks like:
http://server:port/polls