I
I
Ivan Kambur2020-11-22 23:36:38
Django
Ivan Kambur, 2020-11-22 23:36:38

Django doesn't render the url correctly. What's wrong?

Started learning Django and ran into a problem. When I open the site in a browser, it gives me the wrong URL. More precisely, at first everything is correct, " 127.0.0.1:8000 " opens, but I made navigation buttons there

<ul>
            <a href="{% url 'index' %}"><i class="fas fa-home"></i><li>Главная</li></a>
            <a href="{% url 'about' %}"><i class="fas fa-address-card"></i><li>Про нас</li></a>
            <a href=""><i class="fas fa-address-book"></i><li>Контакты</li></a>
        </ul>

And when I click on the "About Us" button, instead of " 127.0.0.1:8000/about " I get " 127.0.0.1:8000/about/about ", then when I click on "Home", I don't get thrown to " 127.0. 0.1:8000 ", but throws at " 127.0.0.1:8000/about "

Below is the code from the urls.py file
urlpatterns = [
    path('', views.index, name='index'),
    path('about', views.about, name='about'),
]

Code from main urls.py
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('index.urls')),
    path('about', include('index.urls')),
]  + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

views.py
from django.shortcuts import render
from django.http import HttpResponse


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

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


This is what it says in the source code in the browser
<ul>
            <a href="/about/"><i class="fas fa-home"></i><li>Главная</li></a>
            <a href="/about/about"><i class="fas fa-address-card"></i><li>Про нас</li></a>
            <a href=""><i class="fas fa-address-book"></i><li>Контакты</li></a>
        </ul>


Well, just in case, settings.py
from pathlib import Path

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.1/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '[email protected]%#nmjbdt4e$##mvevah)#v8pf7!v5x20p^u1rtr0u4ub7'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = [
    'index',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'MyWork.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'MyWork.wsgi.application'


# Database
# https://docs.djangoproject.com/en/3.1/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    }
}


# Password validation
# https://docs.djangoproject.com/en/3.1/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]


# Internationalization
# https://docs.djangoproject.com/en/3.1/topics/i18n/

LANGUAGE_CODE = 'ru'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.1/howto/static-files/

STATIC_ROOT = os.path.join(PROJECT_ROOT, 'static')

STATIC_URL = '/static/'


STATICFILES_DIRS = [
    BASE_DIR, "static",
]

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
Web Dentist, 2020-11-23
@vanya02900

As far as I understand, you indicated in the main urls file Correct on )
path('about/', include('app.urls.py'))
path('', include('app.urls.py')

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question