how to include urlpatterns in django? - 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.

Related

ImportError: No module named urls

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})]

301 redirect using Django urls.py

Hi I'm looking to setup some 301 redirects for some urls that have been changed on a site for a client.
I was thinking the following might work:
from django.conf.urls import patterns, include, url
from django.contrib import admin
from django.views.generic import TemplateView, RedirectView
from cms.sitemaps import CMSSitemap
admin.autodiscover()
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^contact/', include('contact.urls')),
url(r'^case-study/', include('casestudies.urls')),
url(r'^contact-us/', include('contact.urls')),
url(r'^news/', include('news.urls')),
url(r'^sitemap.xml$', 'django.contrib.sitemaps.views.sitemap', {'sitemaps': {'cmspages': CMSSitemap}}),
url(r'^', include('cms.urls')),
url(r'^robots\.txt$', TemplateView.as_view(template_name='robots.txt', content_type='text/plain')),
url(r'^old-link/$', RedirectView.as_view(url='new-link/'), name='new-link'),
)
This seems to make sense to me but this doesn't seem to work. Now i am extremely new to Django so apologies if this is a really simple query.

How to place "any url" at the end of urlpattern list

I want to place url(r'^', include('article.urls')), at the end of the urlpatterns list, because after urlpatterns I have url for MEDIA_ROOT:
if settings.DEBUG:
urlpatterns += patterns('',
(r'^media/(?P<path>.*)$', 'django.views.static.serve', {
'document_root': settings.MEDIA_ROOT}))
So I need url(r'^', include('article.urls')) to be the last.
I tried
urlpatterns += patterns('', (r'^'), include('article.urls'),
)
But it's not working.
main urls.py
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'firstapp.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
url(r'^redactor/', include('redactor.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'^basicview/', include('article.urls')),
url(r'^auth/', include('loginsys.urls')),
url(r'^', include('article.urls')),
)
if settings.DEBUG:
urlpatterns += patterns('',
(r'^media/(?P<path>.*)$', 'django.views.static.serve', {
'document_root': settings.MEDIA_ROOT}))
article urls.py
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'firstapp.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
url(r'^articles/$', 'article.views.articles'),
url(r'^articles/(?P<article_id>\d+)/$', 'article.views.article'),
url(r'^articles/addlike/(?P<article_id>\d+)/$', 'article.views.addlike'),
url(r'^articles/addcomment/(?P<article_id>\d+)/$', 'article.views.addcomment'),
url(r'^page/(\d+)/$', 'article.views.articles'),
url(r'^', 'article.views.mainPage'),
)
You may need this in your urls.py:
url(r'^.*', include('article.urls')),
And in your article.urls.py you need this:
url(r'^.*', 'article.views.mainPage'),
You may want to check this question & answer

Django templates folders

I'm experimenting with Django, and figuring out how to set urls.py, and how the URLs work.
I've configured urls.py in the root of the project, to directs to my blog and admin.
But now I want to add a page to my home, so at localhost:8000.
So I've added to following code to the urls.py in the root of the project:
from django.views.generic.simple import direct_to_template
urlpatterns = patterns('',
(r"^$", direct_to_template, {"template": "base.html"}),
)
The problem is that it searches for the template in blog/templates/...
Instead of the templates folder in my root. Which contains the base.html.
Full urls.py:
from django.conf.urls import patterns, include, url
from django.views.generic.simple import direct_to_template
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
(r"^$", direct_to_template, {"template": "base.html"}),
url(r'^blog/', include('hellodjango.blog.urls')),
url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
url(r'^admin/', include(admin.site.urls)),
(r'^tinymce/', include('tinymce.urls')),
)
Am I overlooking something?
Did you set TEMPLATE_DIRS in your settings.py? Check and make sure it is set up correctly with absolute paths. This is how I make sure it is properly set:
settings.py
PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))
TEMPLATE_DIRS = (
# Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
os.path.join(PROJECT_ROOT, 'templates').replace('\\','/'),
)
# List of callables that know how to import templates from various sources.
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
# 'django.template.loaders.eggs.Loader',
)
This way, I have a templates folder in my project root that is used for non-app templates and each app has a templates/appname folder inside the app itself.
If you want to use a template from the root template folder, you just give the name of the template like 'base.html' and if you want to use an app template, you use 'appname/base.html'
Folder structure:
project/
appname/
templates/
appname/ <-- another folder with app name so 'appname/base.html' is from here
base.html
views.py
...
templates/ <-- root template folder so 'base.html' is from here
base.html
settings.py
views.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('',
url(r'^blog/', include('hellodjango.blog.urls')),
url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'^tinymce/', include('tinymce.urls')),
)
urlpatterns += patterns(
'django.views.generic.simple',
(r'^', 'direct_to_template', {"template": "base.html"}),
)
I would re-organize the urls as such:
urlpatterns = patterns('',
(r'^admin/doc/', include('django.contrib.admindocs.urls')),
(r'^admin/', include(admin.site.urls)),
(r'^tinymce/', include('tinymce.urls')),
(r'^blog/', include('hellodjango.blog.urls')),
(r'^$', direct_to_template, {"template": "base.html"}),
)
Patterns are matched by their specificity, so I tend to put the more specific patterns first. Otherwise you might see some unexpected behavior. Give that a try, and if it's still loading a template from your blog on a request to /, we'll dig deeper.
I think it depends what you want your home page to be. If its simply a page with links off to other parts of your site then catherine's answer is a nice clean way.
If you want the root of your site to be your blog for example I would do this:
urlpatterns = patterns('',
# Django Admin
url(r'^admin/', include(admin.site.urls)),
# Tiny MCE Urls
url(r'^tinymce/', include('tinymce.urls')),
# Other App
url(r'^other/', include('projectname.other.urls', namespace='other')),
# Blog App
url(r'^', include('projectname.blog.urls', namespace='blog')),
)
Also don't forget to name space your url includes: https://docs.djangoproject.com/en/dev/topics/http/urls/#url-namespaces
you can refer the my solution
Django==3.2.5
https://stackoverflow.com/a/68420097/10852018

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.