I am using django 1.9 version and I wanted to implement ajax search in my application. In the documentation it is says to add the urls to the root url patterns.
url(r'^ajax_search/',include('ajax_search.urls')),`
Then I am getting an import error as follows:
File "/usr/local/lib/python2.7/dist-packages/django_ajax_search-1.5.1-py2.7.egg/ajax_search/urls.py", line 1, in <module>
from django.conf.urls.defaults import *
ImportError: No module named defaults
Can any one help me solve this issue?
django.conf.urls.defaults has been removed from Django 1.6 onwards.
django-ajax-search package was last updated in 2013. The package has not been updated for a long and will not work smoothly for Django 1.9
Either you can find another package or you can manually update it.
django.conf.urls.defaults is deprecated in Django 1.4, later removed in Django 1.6. Read this. And the package you are using has the urls not compatible with Django 1.9. According to the Django 1.9 documentation you should define your urls.py as,
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^articles/2003/$', views.special_case_2003),
url(r'^articles/([0-9]{4})/$', views.year_archive),
url(r'^articles/([0-9]{4})/([0-9]{2})/$', views.month_archive),
url(r'^articles/([0-9]{4})/([0-9]{2})/([0-9]+)/$', views.article_detail),
]
UPDATE:
You can modify your urls.py as below to make this working,
from django.conf.urls import url, include
from ajax_search import views as as_views
ajax_search_urlpatterns = [
url(r'^xhr_search$','as_views.xhr_search'),
url(r'^search/', 'as_views.search'),
]
urlpatterns = [
url(r'^ajax_search/',include(ajax_search_urlpatterns)),
]
Related
I'm using Django 1.9
from django.contrib admin
from django.conf.urls import include, url
from slack.views import DRSWebhookTransactionView, DRSMessageView
from django.urls import path
api_patterns = ([
path('web/', DRSWebhookTransactionView.as_view()),
path('events/', DRSMessageView.as_view()),
], 'api')
urlpatterns = [
url(r'^admin/', admin.site.urls),
path('api/v1/', include(api_patterns)),
]
After running python manage.py runserver:
from django.urls import ( # noqa
ModuleNotFoundError: No module named 'django.urls'
I'm getting this error after I tried including path. If I don't include path it's not showing the error for the 'path', it's still showing the same error. Can someone tell how can I rewrite this program? and tell me what I'm doing wrong?
for django 1.9
from django.conf.urls import url
refer this
What is the probelm ?I am getting lot of stress with this code.
MY CODE::::
from django.contrib import admin
from django.urls import path
from basicapp import views
urlpatterns = [
path('',views.index,name='index'),
path('admin/', admin.site.urls),
path('formpage/',views.form_name_view,name='form_name'),
]
PROBLEM///ERROR::::
from django.urls import path
ImportError: cannot import name path
django.urls.path is new in Django 2.0. Make sure you use Django 2.0 or if you have to stick to <2.0 use django.conf.urls.url.
Docs for path (2.0): https://docs.djangoproject.com/en/2.1/ref/urls/#path
Docs for url (<2.0): https://docs.djangoproject.com/en/1.11/ref/urls/#url
It helps to use an editor that manages imports for you like PyCharm or Visual Code or Vi with appropriate plugins or many other.
I'm following this tutorial for install django-tinymce4-lite. At the end of the tutorial there are the indications to install django-filebrowser-no-grappelli.
I use Django 2.1.1 but even though I've followed all the indications, after the installation of the file browser was shown this message:
File
"/var/www/html/dev/miosito/django/beautifulsite_v0.1.1/djangosite/djangosite/urls.py",
line 25, in
path('admin/filebrowser/', include(site.urls)), NameError: name 'site' is not defined
Here there is 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
from filebrowser.sites import site #sorry I've forgot this
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('testapp.urls')), #app for my tests
path('tinymce/', include('tinymce.urls')),
path('admin/filebrowser/', include('site.urls')),
]
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
What I've wrong?
EDIT after Yeo correction:
I've add the string that I've forgot and I've correct
path('admin/filebrowser/', include(site.urls)),
with
path('admin/filebrowser/', include('site.urls')),
but now I've this new error:
ModuleNotFoundError: No module named 'site.urls'; 'site' is not a
package
Try the following: (remove the include)
# ...
from filebrowser.sites import site
# ...
urlpatterns = [
# ...
path('admin/filebrowser/', site.urls),
# ...
]
Always refer to the package official documentation, when you encounter errors specific to the package itself. (In this case is django-filebrowser, although the main repo seems to be at django-filebrowser-no-grappelli). Blog sometime gets outdated easily. For example the guide from your link does not specify what Django version they are using. (Looking from the way the tutorial was written include, it seems to be Django<1.9 (reference)).
If you're using Django>=2, then the official document should explain the correct way to install this package.
I have a problem with Django, I created a 'login' app and added the URL on mysite/urls.py as below:
from django.conf.urls import include, patterns, url
from django.contrib import admin
from django.urls import path
urlpatterns = [
path('admin/', admin.site.urls),
url(r'^polls/', include('polls.urls')),
url(r'^user-auth/', include('user_auth.urls')),
url(r'^file-upload/', include('file_uploader.urls')),
url(r'^pagination/', include('pagination.urls')),
patterns('login.views',
url(r'^login/', 'loginView'),
url(r'^greeting/', 'formView'),
url(r'^logout/', 'logoutView')
)
]
However, when I started the server, I received the message on console as:
File "/home/win/Python/mysite/mysite/urls.py", line 16, in <module>
from django.conf.urls import include, patterns, url
ImportError: cannot import name 'patterns'
Do you meet any problem like this? and any resolution do you have to resolve it.
Please please help me.
Thanks
In the latest release of Django (as of this post), patterns is not used.
You can use re_path for the same effect. For Example:
from django.urls import include, re_path
from django.contrib import admin
from myapp.views import *
urlpatterns = [
re_path(r'^admin', include(admin.site.urls)),
re_path(r'^$', home, name='home'),
]
For more information please follow: Documentation
FYI patterns has been removed in Django 1.10. See release 1.10 notes:
https://docs.djangoproject.com/en/2.0/releases/1.10/
If you want to use earlier versions (but i don't see why you would want to do it) , anything below that i.e. 1.9 should be ok, but do note it has been slotted for deprecation since 1.8 i think.
And if you're using django, especially if you are new, I don't see why you would want to use your own login app. Django has a very mature and customizable auth backend. For starters, I strongly suggest you check it out. Useful examples of usage at https://djangobook.com/authentication-views/
If you are using the latest version of Django, then patterns is has been deprecated. You would simply use URL and/or Path depending on if you are on 1.11 or 2.0. If you require patterns, then you would need to downgrade to an earlier Django version.
I'm new to Django, have been doing several tutorials to get really comfortable with the structuring, and am now running through the official tutorial.
I've created an polls App, which has a polls/views.py file as follows:
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def index(request):
return HttpResponse("Hello, World. You're at the polls index.")
I've also created an App URLconf file polls/urls.py with the following code:
from django.conf.urls import url
from . import views
url_patterns = [
url(r'^$', views.index, name='index'),
]
This is pretty much exactly as done in the Django tutorial.
My issue is when I'm specifying url routes in the main projectname/url.py file on a project level as such:
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^polls/', include('polls.urls')),
url(r'^admin/', admin.site.urls),
]
When doing this, I get the following error:
django.core.exceptions.ImproperlyConfigured: The included URLconf '<module 'polls.urls' from 'ProjectFolder\\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.
This is how the official Django tutorial dictates it to be done. However, if I explicity import the polls/views.py file from the app, I can accomplish the task as follows:
from django.conf.urls import url
from django.contrib import admin
from polls import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^polls/', views.index),
]
My immediate concern is the import of every app/urls file I ever create being necessitated by this approach, as well as the obvious divergence from the official Django instruction.
I hesitated to even ask this question because I feel that such a fundamental issue has bound to have an easy fix. Any help would be greatly appreciated.
To clarify, I can get around the error by explicitly importing the view files from apps. Whenever using the Django documentation-described approach of using the include() function I receive the error. I can appreciate the value of this function, and would like to know why is giving me the error described above.
Just writte urlpatterns = [ .. and not url_patterns in your poll.views.py.