I have been trying out the django tutorial and stuck with this error. I am in the 3rd page of the tutorial and everything was going good until i encountered this error Django Tutorial. I was following the exact steps in the tutorial. This is the error i got
ImproperlyConfigured at /polls
The included urlconf <module 'polls.urls' from 'C:\\Python34\\mysite\\polls\\urls.py'> doesn't have any patterns in it
I am pasting my code here.
ROOT_URLCONF = 'mysite.urls'` in settings.py,
from django.conf.urls import patterns, url
from polls import views
urlpattern = patterns('',
url(r'^$',views.index,name='index')
)`
inside MYSITE/POLLS/URLS.PY
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'mysite.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
url(r'^polls/', include('polls.urls')),
url(r'^admin/', include(admin.site.urls)),
)
INSIDE MYSITE.URLS.PY
Sorry if i missed out anything that i had to mention to get a clear picture. Any help would be appreciated.
The first file has urlpattern instead of urlpatterns.
Related
I am working from the Python Crash Course from no starch by Eric Matthes. The book is working with Python 2.0 and I am trying to essentially convert it to work with Python 3.0. I am having an issue with include(). The error I am receiving in command prompt is:
django.core.exceptions.ImproperlyConfigured: passing a 3-tuple to
include() is not supported. Pass a 2-tuple containing the list of
patterns and app_name, and provide the namespace argument to include()
instead.
Here is my code:
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'', include('learning_logs.urls', namespace='learning_logs')),
]
First of all, you should always mention your Django version when you are asking questions.
As per your code, this is the old way of doing things. If I'm not wrong, starting from Django 2 you have to do this in a little different way.
There are 2 ways to solve it.
First way:
change your code to this:
from django.conf.urls import include, url
from django.contrib import admin
learning_logs_patterns = ([
url(...),
], 'learning_logs')
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^polls/', include(learning_logs_patterns, namespace='learning_logs')),
]
Second way:
in mysite.urls.py
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'', include('learning_logs.urls')),
]
in learning_logs.urls.py
app_name = 'polls'
urlpatterns = [...]
For Details see the documentation : https://docs.djangoproject.com/en/3.1/releases/1.9/#passing-a-3-tuple-or-an-app-name-to-include
A little suggestion: Starting from Django 2 there is a better way for declaring urls.It's called path(). See documentation for details: https://docs.djangoproject.com/en/3.1/ref/urls/
I am working with django 1.9 and I am currently coding - in Windows Command Prompt - python manage.py makemigrations and the error:
AttributeError: 'str' object has no attribute 'regex'
I have tried coding:
url(r'^$', 'firstsite.module.views.start', name="home"),
url(r'^admin/', include(admin.site.urls)),
url(r'^$', 'django.contrib.auth.views.login', {'template_name': 'login.html'}, name='login'),
url(r'^signup/$', 'exam.views.signup', name='signup'),
url(r'^signup/submit/$', 'exam.views.signup_submit', name='signup_submit')
in urls.py and the error is keeps coming up.
This is my first time coding in django, so my expertise is very limited. Thank you in advance.
This is the whole urls.py:
from django.conf.urls import patterns, include, url
import django
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
from django.conf.urls.static import static
from django.conf import settings
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'firstsite.views.home', name='home'),
# url(r'^firstsite/', include('firstsite.foo.urls')),
# Uncomment the admin/doc line below to enable admin documentation:
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
#url(r'^admin/', include(admin.site.urls)),
django.conf.urls.handler400,
url(r'^$', 'firstsite.module.views.start', name="home"),
url(r'^admin/', include(admin.site.urls)),
url(r'^$', 'django.contrib.auth.views.login', {'template_name': 'login.html'}, name='login'),
url(r'^signup/$', 'exam.views.signup', name='signup'),
url(r'^signup/submit/$', 'exam.views.signup_submit', name='signup_submit'),
)
Also make sure to remove the beginning empty url pattern--can be overlooked when migrating your urls.
urlpatterns = ['', # <== this blank element ('') produces the error.
...
]
tl;dr
For the curious, I found this out by adding a warning to the check_pattern_startswith_slash method in the django.core.checks.urls module:
def check_pattern_startswith_slash(pattern):
"""
Check that the pattern does not begin with a forward slash.
"""
if not hasattr(pattern, 'regex'):
warning = Warning(
"Invalid pattern '%s'" % pattern,
id="urls.W002",
)
return [warning]
And, sure enough, I got a bunch of warnings like this:
?: (urls.W002) Invalid pattern ''
Firstly, remove the django.conf.urls.handler400 from the middle of the urlpatterns. It doesn't belong there, and is the cause of the error.
Once the error has been fixed, you can make a couple of changes to update your code for Django 1.8+
Change urlpatterns to a list, instead of using patterns()
Import the views (or view modules), instead of using strings in your urls()
You are using the same regex for the start and login views. This means you won't be able to reach the login views. One fix would be to change the regex for the login view to something like ^login/$
Putting that together, you get something like:
from firstsite.module.views import start
from exam import views as exam_views
from django.contrib.auth import views as auth_views
urlpatterns = [
url(r'^$', start, name="home"),
url(r'^admin/', include(admin.site.urls)),
url(r'^login/$', auth_views.login, {'template_name': 'login.html'}, name='login'),
url(r'^signup/$', exam_views.signup, name='signup'),
url(r'^signup/submit/$', exam_views.signup_submit, name='signup_submit'),
]
Remove the beginning empty Url patterns and
also remove
django.conf.urls.handler400,
from your urls.py this will solve your problem.
For Django 2
from django.urls.resolvers import get_resolver, URLPattern, URLResolver
urls = get_resolver()
def if_none(value):
if value:
return value
return ''
def print_urls(urls, parent_pattern=None):
for url in urls.url_patterns:
if isinstance(url, URLResolver):
print_urls(url, if_none(parent_pattern) + if_none(str(url.pattern)))
elif isinstance(url, URLPattern):
print(f"{url} ({url.lookup_str})")
print('----')
print_urls(urls)
I'm following this tutorial to learn Django. I'm a total starter.
My urls.py file has following code
from django.conf.urls import include, url
from django.contrib import admin
from . import views
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^polls/', include('polls.urls')),
url(r'^admin/', include(admin.site.urls)),
]
My views.py file is
from django.shortcuts import render
def index(request):
return HttpResponse("Hello, world. You're at the polls index.")
When i try to access th url http://127.0.0.1:8000/polls/ in my system it gives a page not found message. What am i doing wrong?
Is it a problem with the version difference?
Here is the screenshot of the error
This error comes if your urls.py file in polls directory does not have this pattern. Create a file urls.py your polls directory and add following url pattern
from django.conf.urls import patterns, url
from polls import views
urlpatterns = patterns('',
# ex: /polls/
url(r'^$', views.index, name='index'),
)
Ok, so if you want to use /polls/ with this urls,
your polls.urls should look similar to this ( for django1.8):
from django.conf.urls import url
from polls.views import BasePollView, AnotherPollView
urlpatterns = [
url(r'^$', BasePollView.as_view()),
url(r'^another-url/$', AnotherPollView.as_view())
]
/polls/ -> BasePollView
/polls/another-url/ -> AnotherPollView
I started going through tut but then got stuck on the page.
tutorial page 3 - django
I exactly followed the tut to the point where it asks to goto localhost:8000/polls/.
There starts the problem, it is not working, states an error
Please, tell how to correct the error.
That page is a 404 page not found. This suggests to me that you have either not configured your URL's correctly or it's missing.
/project/urls.py
urlpatterns = patterns('',
url(r'^polls/', include('polls.urls')),
)
Where polls.urls points to your app URL at /polls/urls.py
So now in your polls/url.py add the index as described.
from django.conf.urls import patterns, url
from polls import views
urlpatterns = patterns('',
url(r'^$', views.index, name='index')
)
https://docs.djangoproject.com/en/1.6/intro/tutorial03/#write-your-first-view
I have a question about urls.py in Django. I am building a blog from scratch as a way of learning Django myself. In the main urls.py file, I have specified the include path to my app's urls.py.
from django.conf.urls import patterns, include, url
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
(r'^', include('myblog.urls')),
)
In the app (called myblog), the urls.py reads as follows:
from django.conf.urls.defaults import *
from models import blogmodel
from django.contrib import admin
urlpatterns = patterns('',
(r'^login/', include(admin.site.urls)),
(r'^$', include('myblog.views.getLatest')),
)
where getLAtest is the function in my views.py. The error says No module named getLatest
Here's my views.py,
from django.shortcuts import render_to_response
from myblog.models import blogdb
def getLatest(request):
post = blogdb.objects.all()
sorted_post = post.order_by('-served_date')
return render_to_response('blogs.html', {'posts':sorted_post})
Any help is appreciated. Thanks in advance..
You are using the wrong directive; include() is used to include another package; Django will look for a urls.py within the package myblog.views.getLatest when you use that directive.
You want to name the view itself instead:
urlpatterns = patterns('',
(r'^login/', include(admin.site.urls)),
(r'^$', 'myblog.views.getLatest'),
)
Note: no include() is being used.
Try updating this:
urlpatterns = patterns('',
(r'^login/', include(admin.site.urls)),
(r'^$', include('myblog.views.getLatest')),
)
to this:
urlpatterns = patterns('',
(r'^getLatest/$', 'myblog.views.getLatest'),
)
include is meant to read in another urls.py file, where you you are wanting to execute a specific view function.