Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django '/' only homepage url error

I am using Django 2.0 and now I have no idea how to make an 'empty' url for the homepage. Meaning, I want it to route for web.com/ or web.com. I tried this code but it does not work:

urlpatterns = [
    path('admin/', admin.site.urls),
    path('/', include('post.urls'))
]

...and post.urls

urlpatterns = [
    path('', views.index, name='index')
]

And the error I get when I make a request to localhost:8000:

Request URL: http://localhost:8000/ Using the URLconf defined in myblog.urls, Django tried these URL patterns, in this order:

  1. admin/

  2. /

The empty path didn't match any of these.

I did sort of find a workaround by setting path to an empty string '' on both but I am not sure if it is recommended or what errors it might cause. Help is much appreciated. Thank you :-).

like image 780
ICanKindOfCode Avatar asked Jan 02 '18 16:01

ICanKindOfCode


People also ask

How do I change the default URL in Django?

Set up app folder's urls.py and html files In the same directory, you should have a file named views.py. We will create a function called index which is what makes the http request for our website to be loaded. Now, we've set it up such that http://127.0.0.1:8000/homepage will render the HTML template index.

How do I reference a URL in Django?

The most basic technique to name Django urls is to add the name attribute to url definitions in urls.py . Listing 2-16 shows how to name a project's home page, as well as how to reference this url from a view method or template.

Is URL deprecated in Django?

will the projects that use it have to change their source code? Yes, if they upgrade to django-4.0, url will no longer be available. Typically if something is marked deprecated, it is removed two versions later, so in django-4.0, since after django-3.2, django-4.0 will be released.

What is re_path in Django?

re_path is an implementation of the 'old' way of handling urls, which was previously (version <2) done by url from django. conf. urls . See the paragraph in Django 2.0 release notes about this.


2 Answers

You don't need "/" for Home url, just leave both the path with "". The home url 127.0.0.1:8000/ is nevertheless is same as 127.0.0.1:8000. This URL pattern will work for the home page.

urls.py

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('post.urls'))
]

and posts.urls

urlpatterns = [
    path('', views.index, name='index')
]
like image 110
Jai Singhal Avatar answered Oct 17 '22 10:10

Jai Singhal


There are plenty of references to blank URL patterns in the Django 2.0 doccumentation.

Django 2.0 Docs URLs section

Furthermore, the slash you added on 'admin/' when viewing from browser is not required. The url dispatcher will strip the trailing slashes on your URL confs, but it wont be stripped on the Web request. This means it wont find a match in your case, try localhost:8000/admin

like image 34
Waterdrip Avatar answered Oct 17 '22 09:10

Waterdrip