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)
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'm reading the book "Learning Python" and I've come across an issue with the chapters project. The project uses an older version django so I've had to modify some items, but this one really has me stuck. Basically the auth_views.login needs to become auth_views.LoginView. Same for logout. I can't figure out how to "convert" the code over to the new method.
I've taken a look at the documentation and the docs don't show an apples to apples example of the projects workflow (using regex urls). This throws a position argument error (takes 1, 2 were given)
tried replacing :
url(r'^login/$',
auth_views.login,
kwargs={'template_name': 'admin/login.html'},
name='login'),
with :
url(r'^login/$',
auth_views.LoginView(template_name='admin/login.html'),
name='login'),
to no avail.
Below is a full snippet.
# regex/urls.py
from django.conf.urls import include, url
from django.contrib import admin
from django.contrib.auth import views as auth_views
from django.core.urlresolvers import reverse_lazy
from entries.views import HomeView, EntryListView, EntryFormView
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^entries/$', EntryListView.as_view(), name='entries'),
url(r'^entries/insert$',
EntryFormView.as_view(),
name='insert'),
# here lies the troublemaker
url(r'^login/$',
auth_views.login,
kwargs={'template_name': 'admin/login.html'},
name='login'),
# as well as here
url(r'^logout/$',
auth_views.logout,
kwargs={'next_page': reverse_lazy('home')},
name='logout'),
url(r'^$', HomeView.as_view(), name='home'),
]
my app urls.py is:
from django.urls import path
from . import views
from django.conf.urls import (handler400, handler403, handler404,
handler500)
app_name = "bca"
handler404 = 'my_app.views.handler404'
urlpatterns = [
path("", views.index, name='index'),
path("login/", views.login_request, name='login'),
path("register/", views.register, name='register'),
path("logout/", views.logout_request, name='logout'),
path("<match>", views.match, name='match'),# this is being preferred first...
]
when ever i try admin/ in url it gives a value error
What should I do?
Your URL patterns are for /admin/, /login/ and so on (all with a trailing slash). The error shows you are going to /admin (without a trailing slash). If you add the slash, you should see the Django admin.
The default behaviour in Django is to redirect /admin (without a trailing slash) to /admin/. However, when you add a catch-all pattern like path("<match>", ...), this behaviour stops working. Therefore you should think carefully about whether you really want a catch-all pattern. An alternative would be to display the content from the match view on your 404 page.
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.
I ran into a problem loading the admin section of my django site with the following error:
No FlatPage matches the given query.
I managed to solve the problem, but I'm attempting to understand why it solved the problem. In my urls.py file, I moved the url(r'', include('django.contrib.flatpages.urls')), after the url(r'^admin/', include(admin.site.urls)), and voila, it worked. Can someone explain, briefly, why this would make a relevant difference? Should the admin url always be at the top of the list?
Code the produced the error:
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'mysite.views.home', name='home'),
# url(r'^mysite/', include('mysite.foo.urls')),
url (r'^blog/', include('blog.urls')),
# Uncomment the admin/doc line below to enable admin documentation:
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
url(r'', include('django.contrib.flatpages.urls')),
# Uncomment the next line to enable the admin:
url(r'^admin/', include(admin.site.urls)),
)
This is django 1.4.
The problem is that you have not installed the flatpages application correctly. Django's flatpages application relies on a piece of middleware that intercepts 404 requests. As a result, you do not need to add django.contrib.flatpages.urls to your urls.py.
You received that error because the regex you are using ('') matches all URLs. As a result, the urlpattern never reached ^admin/'.