Начал учить Django, и столкнулся с проблемой. Когда открываю сайт в браузере, мне выдаёт неправильный URL. Точнее сначала всё верно, открывается "
127.0.0.1:8000", но я там сделал кнопки навигации
<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>
И когда я нажимаю на кнопку "Про нас" мне вместо "
127.0.0.1:8000/about" выдаёт "
127.0.0.1:8000/about/about", потом когда нажимаю на "Главная", меня не кидает не на "
127.0.0.1:8000", а кидает на "
127.0.0.1:8000/about"
Ниже код с файла urls.py
urlpatterns = [
path('', views.index, name='index'),
path('about', views.about, name='about'),
]
Код с основного 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')
Вот что пишет в исходном коде в браузере
<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>
Ну и на всякий случай 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 = 'bv@m0%#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",
]