TypeError: 'function' object has no attribute 'getitem' django url part - django

hi I started to learn django newly. I want to upload a photo to my website. I did the other parts but in urls part I got error like this. What should I do?
from django.contrib import admin
from django.conf.urls import patterns, include, url
from django.conf import settings
from django.conf.urls.static import static
from bildirge.views import contact, home
from bildirge.urls import *
from django.conf.urls.static import static
from django.conf import settings
from django.contrib import admin
admin.autodiscover()
from bildirge.views import ProfileImageView, ProfileDetailView, ProfileImageIndexView
urlpatterns = patterns[
url(r'^contact/', contact),
url(r'^home/', home),
url(r'^$', ProfileImageIndexView.as_view(), name='home'),
url(r'^upload/', ProfileImageView.as_view(), name='profile_image_upload'),
url(
r'^uploaded/(?P<pk>\d+)/$', ProfileDetailView.as_view(),
name='profile_image'),
url(r'^admin/', include(admin.site.urls))
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
And error is
TypeError: 'function' object has no attribute 'getitem'
thanks in advance

You've got a stray patterns before the opening of the list of URLs. Remove that.

Related

Preview custom 500 error page in Wagtail?

I've made custom 500 and 404 error pages in Wagtail. I can preview the 404 page by typing in a false url. I'm just wondering how I can preview the 500 page?
The custom page has links to static images that I need to check are working.
My urls.py
from django.conf import settings
from django.conf.urls import include, url
from django.conf.urls.static import static
from django.contrib import admin
from wagtail.contrib.sitemaps.views import sitemap
from wagtail.admin import urls as wagtailadmin_urls
from wagtail.core import urls as wagtail_urls
from wagtail.documents import urls as wagtaildocs_urls
admin.autodiscover()
urlpatterns = [
url(r'django-admin/', admin.site.urls),
url(r'^admin/', include(wagtailadmin_urls)),
url(r'^documents/', include(wagtaildocs_urls)),
url(r'^sitemap\.xml$', sitemap),
url(r'', include('puput.urls')),
url(r'', include(wagtail_urls)),
]
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
if 'debug_toolbar' in settings.INSTALLED_APPS:
import debug_toolbar
urlpatterns += [
url(r'^__debug__/', include(debug_toolbar.urls)),
]
The answer at https://stackoverflow.com/a/24660486/823020 has most of these details. You can make a view that raises a 500 error.
You can add a views.py to any app. In that file (taken directly from the linked answer):
from django.http import HttpResponseServerError
def my_test_500_view(request):
# Return an "Internal Server Error" 500 response code.
return HttpResponseServerError()
Supplement this in your urls.py with:
from django.conf import settings
from django.urls import path
# or for Django 1.x do
# from django.urls import url
from myapp import views
urlpatterns = [
# original content here
]
if settings.DEBUG:
urlpatterns += [
path('test_500/', views.my_test_500_view, name="test_500"),
# or for Django 1.x do
# url(r'^test_500/$', views.my_test_500_view, name="test_500"),
]
If it's not directly related to any Wagtail pages, then a utils Django app can work well for generic shared code.
Just add url with TemplateView to your urls.py:
from django.views.generic import TemplateView
urlpatterns = [
url(r"^admin/", include(wagtailadmin_urls)),
url(r"^documents/", include(wagtaildocs_urls)),
url(r"^500/", TemplateView.as_view(template_name='500.html')),
]

How to define a URL in django that directly points to a static file?

I have a urls.py file that looks like this:
from django.conf.urls import include, url
from django.contrib import admin
from django.conf.urls.static import static
from django.conf import settings
from django.views.generic.base import RedirectView
from django.contrib.staticfiles.templatetags.staticfiles import static
urlpatterns = [
url(r'^admin/', admin.site.urls),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
I want to make a URL (say r'^test/' ) such that when I enter that URL, it directly links to a static file (say 'static/test.html').
A simple way is you can use redirect function to do this.
from django.shortcuts import redirect
def test(request):
return redirect(to="/static/test.html")
and point with your url to this function. I have tested and this works perfectly for me.

FIX ImportError: cannot import name 'patterns'

I am writing documentaton for the API I have already built. I have already installed drfdocs and added it in INSTALLED_APPS then configured the urls yet I get the following logs below. I think the version of rest_framework_docs is importing patterns which I believe its deprecated, new versions of django are no longer using it. How do I fix this?
I have django 1.11 and using python3. I get the following logs when I try to run ther server:
Logs
File "/usr/local/lib/python3.5/dist-packages/rest_framework_docs/urls.py", line 1, in <module>
from django.conf.urls import patterns, include, url
ImportError: cannot import name 'patterns'
Views.py
from django.conf.urls import include, url
from django.contrib import admin
from rest_framework.urlpatterns import format_suffix_patterns
# from organizations.backends import invitation_backend
from rest_framework.authtoken.views import obtain_auth_token
from django.conf.urls.static import static
from django.conf import settings
urlpatterns = [
url(r'api/token/', obtain_auth_token, name='api-token'),
# url(r'^account/', include('accounts.urls', namespace='account')),
url(r'^hr/', include('hr.urls', namespace='hr')),
url(r'^acc/', include('Accounting.urls', namespace='Accounting')),
url(r'^payroll/', include('payroll.urls', namespace='payroll')),
url(r'^bill/', include('bill.urls', namespace='bill')),
url(r'^docs/', include('rest_framework_docs.urls')),
url(r'^proc/', include('procurement.urls', namespace='procurement')),
# url(r'^accounts/', include('organizations.urls')),
# url(r'^authority/', include('authority.urls')),
url(r'^organization/', include('organizations.urls')),
url(r'^admin/', admin.site.urls),
# url(r'^invitations/', include(invitation_backend().get_urls())),
]
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

How would I write this url pattern in python 3.6/django1.11? (it is currently in python 2.7/django1.7)

I am very new, and I am doing a tutorial, that is a little bit old. I keep getting an error that this cannot import name 'patterns' then something about include, then syntax and so on. So what is wrong this section? How would I write it current day? Thank you for your time.
from django.conf.urls import patterns, include, url
from django.contrib import admin
from djangonote.views import home_view
urlpatterns = patterns('',
url(r'^$', home_view, name='home'),
url(r'^notes/', include('notes.urls', namespace='notes')),
)
The reply below fixed that issue, thank you Exprator!
I now get the issue:
NameError: name 'notes' is not defined.
What does that mean?
Ty for your time.
from django.conf.urls import include, url
from django.contrib import admin
from djangonote.views import home_view
urlpatterns = [
url(r'^$', home_view, name='home'),
url(r'^notes/', include('notes.urls', namespace='notes')),
]

Django URL mapping - NameError: name X is not defined

[A similar question was asked, but not marked as answered, here. I considered continuing that thread but the website told me I'm only supposed to post an answer, so it seems I have to start a new topic.] I'm trying to follow this tutorial and I'm having problems with the URL mapping. Specifically with the part described as "So best practice is to create an “url.py” per application and to include it in our main projects url.py file". The relevant, I hope, part of the folder structure, which arose by following steps of the tutorial to the letter (if possible; usage of the 'patterns' module was impossible for example) and using Django 1.10 is the following:
myproject/
myapp/
urls.py
views.py
myproject/
urls.py
The myproject/urls.py is as follows:
from django.conf.urls import include, url
from django.contrib import admin
admin.autodiscover()
from myapp.views import hello
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^myapp/', include(myapp.urls)),
]
The myapp/urls.py is as follows:
from django.conf.urls import include, url
urlpatterns = [
url(r'^hello/', myapp.views.hello),
]
The myapp/views.py is as follows:
from django.shortcuts import render
def hello(request):
return render(request, "hello.html", {})
However, running 'python manage.py runserver' results in the following error:
url(r'^myapp/', include(myapp.urls)),
NameError: name 'myapp' is not defined
INSTALLED_APPS in settings.py contains 'myapp'.
I'd be greatful for any tips on how to deal with the NameError! [Or any tips whatsoever that anyone might consider to be helpful!]
You have the NameError because you are referencing myapp in myproject/urls.py but haven't imported it.
The typical approach in Django is to use a string with include, which means that the import is not required.
url(r'^myapp/', include('myapp.urls')),
Since you have move the hello URL pattern into myapp/urls.py, you can remove from myapp.views import hello from myproject/urls.py.
Once you've made that change, you will get another NameError in myapp/urls.py. In this case, a common approach is to use a relative import for the app's views.
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^hello/$', views.hello),
]
Make sure you have imported following modules to urls.py.
from django.conf.urls import url
from django.contrib import admin
in django 2.0
use these
from django.contrib import admin
from django.urls import path
from first_app import views
urlpatterns = [
path('',views.index, name="index"),
path('admin/', admin.site.urls),
]
your app URL has to be a string
so, here is how the code should look like.
from django.conf.urls import include, url
from django.contrib import admin
admin.autodiscover()
from myapp.views import hello
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^myapp/', include('myapp.urls')),
]
also, note that from python 2 upward the regular expression is not needed.
change URL to path
from django.conf.URLs import include path
from Django.contrib import admin
admin.autodiscover()
from myapp.views import hello
urlpatterns = [
path('^admin/', include(admin.site.urls)),
path('^myapp/', include('myapp.urls')),
]
In Django 2.1.7 here is the default urls .py file
from django.contrib import admin
from django.urls import path
urlpatterns = [
path('admin/', admin.site.urls),
]
so we need to add this line as well
from django.conf.urls import url
I have followed #Alasdair answers
You have the NameError because you are referencing myapp in myproject/urls.py but haven't imported it.
The typical approach in Django is to use a string with include, which
means that the import is not required.
Unfortunately, it didn't work out(I still got the name X is not defined error). Here is how I do it.
from django.contrib import admin
from django.urls import include
from django.conf.urls import url
from article import urls as article_users
from article import urls as user_urls
urlpatterns = [
path('admin/', admin.site.urls),
path('api/article/', include(article_users)),
path('api/user/', include(user_urls)),
]
Before using the URL command be sure to first import the url from the module Urls. Then try using the runserver.
from django.conf.urls import url
from django.contrib import admin
from django.urls import path