Я делаю веб-приложение в django, я установил URL-путь в файле "urls" приложения, а также настроил представления, но проблема в том, что , когда я нажимаю на пункты меню, когда оно работает, теперь, когда я нахожусь на той странице, когда я нажимаю на нее какой-то другой кнопкой или даже нажимаю на ту же самую опцию меню, это выдает ошибку , кажется, что она не маршрутизируется должным образом. Мне нужно go вернуться домой, а затем go на другой URL-адрес ...
, когда я впервые нажимаю на пункт меню (http://localhost: 3000 / сундук / ) Маршрут устанавливается следующим образом.
когда я щелкаю пункт меню на новой странице, он добавляется к маршруту следующим образом (http://localhost: 3000 / сундук / бицепс ) вместо этого (http://localhost): 3000 / бицепс / ).
Код:
urls.py
от django .contrib администратор импорта
from django.urls import path
from . import views
# from django.conf.urls import url
urlpatterns = [
path("", views.home, name = "HOME"),
path("blog_detail/<int:id>", views.blog_detail, name = "blog"),
# path("about/", views.about, name="about"),
# path("contact/", views.contact, name="contact"),
path("back/", views.back1, name="back"),
# re_path(r'^back/$', views.back1, name="back"),
path("chest/", views.chest, name="chest"),
path("shoulder/",views.shoulder, name="shoulder"),
path("abs/", views.abs, name="abs"),
path("bicep/", views.bicep, name="bicep"),
path("tricep/", views.tricep, name= "tricep"),
path("forearm/", views.forearm, name="forearm"),
path("legs/", views.legs, name="legs"),
path("fullbody/", views.fullbody, name="fullbody"),
path("search/", views.search, name="search"),
]
**views.py**
from django.shortcuts import render
from django.http import HttpResponse
# from .models import Blogpost
# from fitness_blog.models import *
from .models import leg
from .models import chest1
from .models import back
from .models import fullbody1
from .models import bicep1
from .models import tricep1
from .models import shoulder1
from .models import forearm1
from .models import abs1
from .models import chest1
from .models import leg
from .models import shoulder1
from .models import tricep1
from .models import bicep1
from .models import forearm1
from .models import fullbody1
from .models import Blogpost
from django.shortcuts import render, redirect
# Create your views here.
def home(request):
post1 = reversed(Blogpost.objects.all())
# print(post1)
# post1.reverse()
print(post1)
return render(request,"blog/index.html", {'post':post1})
def search(request):
query = request.GET.get('search')
all_blogs= Blogpost.objects.all()
searched_blogs=[]
for item in all_blogs:
word= item.title.lower().split()
if query in word:
searched_blogs.append(item)
print(item.title, "this is filtered.........Mubarak button is working....!!!!!!!!!!!")
for t in searched_blogs:
print(t, "from the list")
print(query)
# for items in all_blogs:
# split_items = items.split(" ")
# print(query, a)
if len(searched_blogs) != 0:
return render(request, "blog/search.html", {'post': searched_blogs, "length":len(searched_blogs)})
else:
print("it is reaching here")
return render(request, "blog/not_found.html", {"length":len(searched_blogs)})
def blog_detail(request, id):
# Blog.objects.filter(pk__in=[1, 4, 7])
blog = Blogpost.objects.filter(post_id=id)[0]
return render(request,"blog/blog_detail.html", {"blog": blog})
def chest(request):
post2 = chest1.objects.all()[0]
# print(post2.post_id)
return render(request, "blog/chests.html", {'post': post2})
def legs(request):
post = leg.objects.all()[0]
print(post)
return render(request, "blog/legs.html", {'post': post})
def back1(request):
post = back.objects.all()[0]
print(post)
return render(request, "blog/legs.html", {'post': post})
def fullbody(request):
post = fullbody1.objects.all()[0]
print(post)
return render(request, "blog/full-body.html", {'post': post})
def bicep(request):
post = bicep1.objects.all()[0]
print(post)
return render(request, "blog/bicep.html", {'post': post})
def tricep(request):
post = tricep1.objects.all()[0]
print(post)
return render(request, "blog/bicep.html", {'post': post})
def shoulder(request):
post = shoulder1.objects.all()[0]
print(post)
return render(request, "blog/shoulder.html", {'post': post})
def forearm(request):
post = forearm1.objects.all()[0]
print(post)
return render(request, "blog/forearm.html", {'post': post})
def abs(request):
post = abs1.objects.all()[0]
print(post)
return render(request, "blog/abs.html", {'post': post})
**"basic.html file"which extends on very page of webapp**
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
<title>Gym Crunch</title>
</head>
<div id="fb-root"></div>
<script async defer crossorigin="anonymous" src="https://connect.facebook.net/en_GB/sdk.js#xfbml=1&version=v6.0"></script>
<script async defer crossorigin="anonymous" src="https://connect.facebook.net/en_GB/sdk.js#xfbml=1&version=v6.0"></script>
<script async defer crossorigin="anonymous" src="https://connect.facebook.net/en_GB/sdk.js#xfbml=1&version=v6.0"></script>
<style>
.img-fluid {
width: 100%;
height: 25%;
}
</style>
{% load static %}
<body style="background-image: url('{% static 'blog/background.jpg' %}');">
<div class="container" xmlns="http://www.w3.org/1999/html">
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
<a class="navbar-brand">Brutal-Lifting</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav">
<li class="nav-item active">
<a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="chest" onclick="goBack()">chest</a>
</li>
<li class="nav-item">
<a class="nav-link" href="back">back</a>
</li>
<li class="nav-item">
<a class="nav-link" href="shoulder">shoulder</a>
</li>
<li class="nav-item">
<a class="nav-link" href="abs">ABS</a>
</li>
<li class="nav-item">
<a class="nav-link" href="bicep">bicep</a>
</li>
<li class="nav-item">
<a class="nav-link" href="tricep">tricep</a>
</li>
<li class="nav-item">
<a class="nav-link" href="legs">legs</a>
</li>
<li class="nav-item">
<a class="nav-link" href="forearm">forearm</a>
</li>
<li class="nav-item">
<a class="nav-link" href="fullbody">Full-Body</a>
</li>
</ul>
</div>
</nav>
{% block body %} {% endblock %}
<script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>
</body>
</html>
Я повторяю, проблема в том, что URL в браузере не обновляется, но добавляет URL следующим образом: http://localhost: 3000 / грудь / бицепс вместо обновления следующим образом: http://localhost: 3000 / бицепс /
спасибо, надеюсь, этой информации достаточно