Django 2.0: Url path to homepage/index

django urls path
django url pass parameter to view
from django urls import path importerror cannot import name 'path
django get url parameters
django url template
django url multiple parameters
django url optional parameter
django reverse

I'm trying to set my django homepage, linked to the root of my website: i.e. http://127.0.0.1:8000/index/ but instead, I keep getting errors where Django is searching for myapp as the homepage: http://127.0.0.1:8000/myapp/. I would just like to land on a homepage with "index" in the url instead of "myapp/"

The error is as follows:

Using the URLconf defined in Main.urls, Django tried these URL patterns, in this order:

admin/
[name='index']
[name='myapp_index']
publications/ [name='publications']
^static\/(?P<path>.*)$
The current path, myapp/, didn't match any of these.

Views.py

def index(request):
    return render(request, 'index.html')

Main/urls

from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static


urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('myapp.urls')),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

myapp/urls

from django.urls import path
from logbook import views

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

Now, if I change the path in Main/urls.py to path('myapp/', include('myapp.urls')), I land on the appropriate homepage, except, I would prefer that there is "index/" listed in the URL instead of "myapp/".

I may be missing something very simple here. This is what I think is my MWE, happy to add more.

I was having the same problem and I was cracking my head finding a solution.

The solution was to erase browser's cache and history I realized that because when I tried with incognito mode worked perfectly!

django.urls functions for use in URLconfs, 1.8 · 1.10 · 1.11 · 2.0 · 2.1 · 2.2 · 3.1 · dev; Documentation version: 3.0 This module is pure Python code and is a mapping between URL path expressions to For example, here's an excerpt of the URLconf for the Django website itself. The named URL 'sports:polls:index' would look for a pattern named 'index' in the​  Django 2.0 url () to path () cheatsheet. Django 2.0 introduced a new way to define URLs, which greatly simplifies how parameters are captured. In earlier versions of Django, you had to use the url () method and pass a regular expressions with named capturing groups to capture URL parameters. In Django 2.0, you use the path () method with path converters to capture URL parameters.

This should be in the main urls.py

path('', views.index, name='index')

instead of my_app/urls.py

URL dispatcher | Django documentation, We're about to build our first webpage: a homepage for your blog! from django.​contrib import admin from django.urls import path urlpatterns = [ path('admin/',  This should be a sequence of django.urls.path() and/or django.urls.re_path() instances. Django runs through each URL pattern, in order, and stops at the first one that matches the requested URL, matching against path_info. Once one of the URL patterns matches, Django imports and calls the given view, which is a Python function (or a class-based

path('myapp/', include('myapp.urls'))
path('index/', include('myapp.urls'))

That might get the desired result.

Writing your first Django app, part 3 | Django documentation, For a web application, the view delivers webpage content and templates; for a RESTful \myclub_root\events\urls.py 1 from django.urls import path 2 from . import views 3 4 I.e., the dot operator is pointing to the index view inside the views.py file that we The re_path() function replaced the url() function in Django 2.0. We just created the home page for our site — an HTML page that displays a number of records from the database and links to other yet-to-be-created pages. Along the way we learned fundamental information about url mappers, views, querying the database with models, passing information to a template from a view, and creating and extending templates.

Did you try following:

Main/urls

 ...
 path(r'^', include('myapp.urls')),
 ...

myapp/urls

 ...
 path(r'^$', views.index, name='index'),
 # path('', views.myapp_index, name='myapp_index'), ## REMOVE THIS ROW ##
 ...

Django URLs · Django Girls Tutorial, A view function that will be called if the URL pattern is detected: views.index , which is the function named index() in the views.py file. The path()  See How Django processes a request for more details. The view argument is a view function or the result of as_view() for class-based views. It can also be an django.urls.include(). The kwargs argument allows you to pass additional arguments to the view function or method. See Passing extra options to view functions for an example.

first you must choose one of below lines in myapp.urls and delete other

you can not set two views for one url patterns

path('', views.index, name='index'),
path('', views.myapp_index, name='myapp_index'),

you have two solution for get index/ as main url

1.change Main.urls like below

path('', include('myapp.urls')),

to

path('index/', include('myapp.urls')),

2.change myapp.urls like below

path('', views.index, name='index'),

to

path('index/', views.index, name='index'),

you actually changed name of urls that using in python codes not path of urls

edit Do you mean index/ like the following?

www.example.com/

so u must do that as above but delete "index/"

so u cant do it as below :

change Main.urls like below

path('', include('myapp.urls')),

to

path('^$', include('myapp.urls')),

or

change myapp.urls like below

path('', views.index, name='index'),

to

path('^$', views.index, name='index'),

Django's Views - Python Django Tutorials, In most circumstances, Django uses the django.urls.path method to achieve this url path Initially Django reads the first ^about/index regular expression and determines it's too specific for Prior to Django 2.0, the only way to define urls paths was through regular expressions and the Empty string (Home page), Matches: Writing your first Django app, part 3¶. This tutorial begins where Tutorial 2 left off. We’re continuing the Web-poll application and will focus on creating the public interface – “views.”

Django Tutorial Part 5: Creating our home page, Add a URL to url platterns: path (", views. home, name="home') Class-based views from django. views.decorators.csrf import cerf exempt Gcsrf exempt def index int (v1)+int (v2) about_template= <html> </head> <title>Home Page3/title​>  from django.conf.urls import url, include from. import views urlpatterns = [ url (r'^$', views.index, name='index'), ] So, just like with the urls.py file in the project directory, mywebsite, there is a list called urlpatterns. Inside of this list, we specify the path to the home page. The r stands for raw string.

Django 2.0 Tutorials | 03, Deploying static files covers some common deployment strategies for static files.

Django Url paths and regular expressions: django , The sitemap framework also has the ability to create a sitemap index that references individual sitemap files, one per each section defined in your sitemaps dictionary. The only differences in usage are: You use two views in your URLconf: django.contrib.sitemaps.views.index() and django.contrib.sitemaps.views.sitemap().

Comments
  • It doesn't work unless I have this is in the myapp/urls file: path('myapp/', views.index, name='index'),
  • This doesn't work. Those are deprecated methods as per Django.
  • How do you access this page - do you click on some link or just type url in address bar? If it is link - did you checked if href accidently contains 'myapp' in url?
  • Also, do you successful access 'publications/' addresses?
  • Its actually the homepage when I start the development server on pycharm. 127.0.0.1:8000. The publications/ link works fine, and the link goes to publications/ in the address bar. I just need the homepage to have "index/" in it but it keeps searching for myapp/ (or in real life, it is "logbook/" that it is looking for. I was using "myapp/" for generic usage.)
  • In your myapp.urls you have two rows pointing to ' ' url. Do you really have both index and myapp_index actions in myapp.views? You should have only one url definition pointing to ' '. So, please, try here to write '^$' (to highlight an empty url), and choose only one definiyion (index or myspp_index, what actually works). Also, in main urls instead of ' ' write '^' to highlight empty prefix.
  • Django is still constantly looking for: "myapp/", even when I am sure I have zero reference to it. I get the error: "The current path, myapp/, didn't match any of these."
  • are u mean as index is be as below? www.example.com/
  • I hear you, and it makes sense. That is what is in the Django tutorials. In my settings.py file, I have "'logbook'," in there, or for this example "'myapp',". The error remains the same" looking for "myapp/", or "logbook/" in my home example. Something is causing django to look for "myapp/" and I cant figure out what file might be driving it.
  • Somewhere that must be typed and it should be in some urls.py file. In final, if you are on Unix, you can grep for 'myapp' string and review all the matches