ImportError: No module named urls - django

I have a django rest project which is built on Django1.7. I need to run it on Django 1.11. When i run
python manage.py migrate
The error is:
ImportError: No module named urls
on url.py line
url(r'^docs/', include('rest_framework_swagger.urls')),
I have already made modifications in url.py file to avoid patterns. The url.py file look like
from django.conf.urls import include,url
from django.conf import settings
from django.conf.urls.static import static
from django.views.generic.base import TemplateView
from django.contrib import admin
admin.autodiscover()
urlpatterns = [
url(r'^grappelli/', include('grappelli.urls')),
url(r'^docs/', include('rest_framework_swagger.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'', include('gcm.urls')),
url(r'^', include('apps.account.urls')),
url(r'^', include('apps.vegetables.urls')),
url(r'^', include('apps.orders.urls')),
url(r'^', include('apps.listings.urls')),
url(r'^', include('apps.rating.urls')),
url(r'^', include('apps.faq.urls')),
url(r'^thank-you/$', TemplateView.as_view(template_name="thankyou.html"), name="thankyou"),
url(r'^/error/$', TemplateView.as_view(template_name="error.html"), name="error"),
url(r'^$', TemplateView.as_view(template_name="index.html"), name="home"),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += [
'',
(r'^static/(?P<path>.*)$', 'django.views.static.serve', {
'document_root': settings.STATIC_ROOT})]
How could i run it?

In urls.py add this at the bottom,
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT
From Django 1.9 it does not support to add the URLs as a string, but it need to be imported as callable. So remove this from your urls.py.
urlpatterns += [ '', (r'^static/(?P<path>.*)$', 'django.views.static.serve', { 'document_root': settings.STATIC_ROOT})]

Related

Why is the route correct but still giving 404 error on Django

I got a 404 error on my website when accessing the correct route, did I do something wrong
urls.py handles the main route
from django.contrib import admin
from django.urls import path,include,re_path
urlpatterns = [
path('admin/', admin.site.urls),
re_path(r'api/facebook/(.+?)', include('apifacebooks.urls')),
path('', include('app.urls')),
path('getcookie', include('app.urls')),
path('change-lang', include('app.urls')),
re_path(r'auth/(.+?)', include('app.urls')),
]
urls.py handles routes in the apifacebooks app
from django.urls import path
from . import views
urlpatterns = [
path('api/facebook/like-post',views.like_post)
]
And when I go to http://localhost:8000/api/facebook/like-post I get a 404 error
Image error 404
My question has been solved, thanks
In your apifacebooks app change the path because you had "api..." double in the path
from django.urls import path
from . import views
urlpatterns = [
path('like-post/',views.like_post)
]
And in the root urls.py just
path('api/facebook/', ....)
In your code, the url pattern would be "http://localhost:8000/api/facebook/api/facebook/like-post".
As explained id Django docs:
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.
Remove "api/facebook/" in "apifacebooks.urls", for example:
Main urls.py
from django.contrib import admin
from django.urls import path,include,re_path
urlpatterns = [
path('admin/', admin.site.urls),
re_path(r'api/facebook/(.+?)/', include('apifacebooks.urls')),
path('', include('app.urls')),
path('getcookie', include('app.urls')),
path('change-lang', include('app.urls')),
re_path(r'auth/(.+?)', include('app.urls')),
]
apifacebooks.urls
from django.urls import path
from . import views
urlpatterns = [
path('like-post',views.like_post)
]
Or you can try to place remove "r'api/facebook/(.+?)'", for example:
from django.contrib import admin
from django.urls import path,include,re_path
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('apifacebooks.urls')),
path('', include('app.urls')),
path('getcookie', include('app.urls')),
path('change-lang', include('app.urls')),
re_path(r'auth/(.+?)', include('app.urls')),
]

Adding a sitemap to Django 1.10.7

I'm having issues getting the Django setup to generate a sitemap for me.
I have added the following to my settings file
'django.contrib.sites',
'django.contrib.sitemaps',
and in my urls file I have the following:
from django.conf.urls import include, url
from django.contrib import admin
from ames import views
from cms.sitemaps import CMSSitemap
admin.autodiscover()
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^contact/', include('contact.urls')),
url(r'^news/', include('news.urls')),
url(r'^sitemap.xml$', 'django.contrib.sitemaps.views.sitemap', {'sitemaps': {'cmspages': CMSSitemap}}),
url(r'^$', views.home),
url(r'^', include('cms.urls')),
]
When deploying these amends I get the following error on the site:
TypeError at /sitemap.xml/
view must be a callable or a list/tuple in the case of include().
Any thoughts would be most welcome.
Amended urls.py file:
from django.conf.urls import include, url
from django.contrib import admin
from cms.sitemaps import CMSSitemap
from django.contrib.sitemaps.views import sitemap
from ames import views
admin.autodiscover()
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^contact/', include('contact.urls')),
url(r'^news/', include('news.urls')),
url(r'^sitemap.xml$', sitemap, {'sitemaps': {'cmspages': CMSSitemap}}),
url(r'^$', views.home),
url(r'^', include('cms.urls')),
]
try it:
from django.contrib.sitemaps.views import sitemap
# you code
url(r'^sitemap.xml$', sitemap, {'sitemaps': {'cmspages': CMSSitemap}}),
and remove
url(r'^sitemap.xml$', 'django.contrib.sitemaps.views.sitemap', {'sitemaps': {'cmspages': CMSSitemap}}),
all information for the solution is in error view must be a callable or a list/tuple in the case of include()

How to serve media file when runserver --insecure?

Is there any way to serve Media files when runserver --insecure
It serves only css and js and image but i need to serve media file with ...
They mention it's not possible with cachefile but no word about media.
You need to tell it to serve your media files. In your urls.py:
from django.conf import settings
if settings.DEBUG == False:
urlpatterns += patterns('',
url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {
'document_root': settings.MEDIA_ROOT,
}),
url(r'^static/(?P<path>.*)$', 'django.views.static.serve', {
'document_root': settings.STATIC_ROOT,
}),
)
Write:
import re
from urllib.parse import urlsplit
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.urls import re_path
from django.views.static import serve
def static(prefix, view=serve, **kwargs):
return [
re_path(r'^%s(?P<path>.*)$' % re.escape(prefix.lstrip('/')), view, kwargs=kwargs),
]
Then in urls.py:
urlpatterns = [
path('admin/', admin.site.urls),
...
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Adding django-directupload to Django project

I was looking to try django-directupload in one of my Django projects, but I'm new to Django and having some trouble with installation. I'm following the installation instructions in the README. I installed django-directupload using sudo pip install django-directupload.
Here is my urls.py, which I don't think is right:
from django.conf.urls.defaults import patterns, include, url
from testproject import settings
import directupload
from django.contrib import admin
directupload.admin.patch_admin()
admin.autodiscover()
urlpatterns = patterns('testproject.database.views',
url(r'^(\d+)/$', 'test_view', name="test_page"),
url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'^directupload/', include(directupload.urls))
)
if settings.DEBUG:
urlpatterns += patterns('',
url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {
'document_root': settings.MEDIA_ROOT,
}),
)
Edit:
So I've gained some insight into overriding admin templates (Thanks ilvar for the link). I copied and pasted the contrib/admin/templates/admin/change_form.html file into the project templates directory in the /admin/nameofmyapp/ subdirectory, and added {% load directupload_tags %}{% directupload_head %} below the rest of the load tags. When I go to the Django admin I get the exception: 'module' object has no attribute 'admin' on line 6 of urls.py.
https://docs.djangoproject.com/en/1.4/ref/contrib/admin/#overriding-admin-templates
urls.py should look like this:
from django.conf.urls.defaults import patterns, include, url
from testproject import settings
from directupload.admin import patch_admin
from django.contrib import admin
patch_admin()
admin.autodiscover()
urlpatterns = patterns('testproject.database.views',
url(r'^(\d+)/$', 'test_view', name="test_page"),
url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'^directupload/', include(directupload.urls))
)
if settings.DEBUG:
urlpatterns += patterns('',
url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {
'document_root': settings.MEDIA_ROOT,
}),
)
My imports were incorrect.

how to include urlpatterns in django?

In my project I have one app which have its own urls.py like this
urlpatterns = patterns('',
(r'^(?P<language>\w+)/$', 'MainSite.views.home_page'),)
(above file is in my application )
I am trying include this file in main(project's) urls.py
like this :
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
(r'', include('myproject.MainSite.urls')),
url(r'^admin/', include(admin.site.urls)),
)
if settings.DEBUG :
urlpatterns += patterns('',
(r'^media/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': settings.MEDIA_ROOT}),
)
but after this I can able to call the MainSite's (app's) view but my admin url is not working
I tried
urlpatterns = patterns('',
(r'^$', include('myproject.MainSite.urls')),
url(r'^admin/', include(admin.site.urls)),
)
but after this this makes admin work but my app's view won't get called,
how do I solve this.
You're including your views at the root level. Since it comes before the urlpattern for the admin, the first urlpattern catches everything, so nothing is ever passed to the admin views.
The simplest fix is to simply reverse the order:
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
(r'', include('myproject.MainSite.urls')),
)
Then, your views will only catch anything the admin doesn't.