I'm having a problem with django's URL system. I'm using Django 3. I have the following in my project's 'urls.py'
urlpatterns = patterns('',
url(r'^$', include('siteadmin.urls')),
)
and this in the 'urls.py' of a django app in the project, called 'siteadmin':
urlpatterns = patterns('',
url(r'^$', views.home, name='home'),
url(r'^register/$', views.register, name='register'),
url(r'^login/$', views.user_login, name='login'),
#...trimmed
)
With this setup, the following happens:
http://localhost/ => works perfectly, renders the 'home' view
http://localhost/register/ => breaks. (gives 404 and django's standard "The current URL, register, didn't match any of these.")
However, when I change the project's 'urls.py' to include the following:
urlpatterns = patterns('',
url(r'^app/', include('siteadmin.urls')),
)
And include /app/ in 1. and 2., both urls work perfectly. That is to say:
localhost/app/ => works perfectly
localhost/app/register => works perfectly.
What am I missing? Why does #2 break in the first case, but not the second?
Remove the dollar sign from the regex in the project urls.py:
urlpatterns = patterns('',
url(r'^', include('siteadmin.urls')),
)
Related
I'm using Django-oscar for my project. After Django-oscar config, I can see localhost:8000 empty, Instead, I want to redirect to localhost:8000/Catalogue Page
urls.py
urlpatterns = [
url(r'^i18n/', include('django.conf.urls.i18n')),
url(r'^admin/', admin.site.urls),
url(r'', application.urls),
]
I expected output is. when i run localhost:8000 it should take to localhost:8000/catalogue
How do I make django to redirect from www.example.com to www.example.com/home ?
Below you can see my urlpaterns and I'm using url(r'', RedirectView.as_view(pattern_name='home', permanent=False))to redirect to my /home page but when it detects a link without / at the end it redirects me to the /home page.
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^home/', views.home, name="home"),
url(r'^surukkam/$',views.shrink,name="surukkam"),
url(r'^(?P<inputURL>[0-9a-zA-Z]+)/$',views.retrieve,name="virivu"),
url(r'', RedirectView.as_view(pattern_name='home', permanent=False))
]
it is redirecting because you missed ^$ at the end of home URL.
urlpatterns = [
url(r'^admin/$', admin.site.urls),
url(r'^home/$', views.home, name="home"),
url(r'^surukkam/$',views.shrink,name="surukkam"),
url(r'^(?P<inputURL>[0-9a-zA-Z]+)/$',views.retrieve,name="virivu"),
url(r'^$', RedirectView.as_view(pattern_name='home', permanent=False))
]
^$ - it is a regular expression that specifies the start and end points of a URL string.
whenever your Django detects the empty string it will direct to this URL.
In my Django project, I find the project urls.py resolve URLs directly
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^test/', include('test.urls')),
]
but I find the app urls.py solution always use
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^new$', views.new, name='new'),
)
when I try to change app's urls.py to
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^new$', views.new, name='new'),
]
or
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
)
urlpatterns += patterns('',
url(r'^new$', views.new, name='new'),
)
also works, so I want to know the meaning of using patterns and which one is better.
Patterns is deprecated since 1.8 (and removed in 1.10)
from the 1.8 docs:
Deprecated since version 1.8:
urlpatterns should be a plain list of django.conf.urls.url() instances instead.
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
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.