Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
105 views
in Technique[技术] by (71.8m points)

Django Template Takes The Information From The Other One

inside of the views.py

def book_detail(request, pk):
    book = get_object_or_404(Library, pk=pk)
    return render(request, 'book_detail.html', context={'book': book})

def admin_book_detail(request, pk):
    book = get_object_or_404(Library, pk=pk)
    return render(request, 'admin_book_detail.html', context={'book': book})

Hello, I am new to Django. I have two views like this. One of them is for normal user and the other one is for admin. But the template of admin takes it's information from the other one. Like, If I change the book_detail, admin_book_detail changes too. But if i change admin_book_detail's information, nothing happens.

inside of admin_book_detail.html

{% block content %}
     {{ block.super }}
    <div style="margin:0 auto;width:1200px;">
        <h1 class="page-header" style="color:black;">{{ book.title }}</h1>

        <h2> This book is <b>{{ book.availability }}</b> to borrow</h2>
        <br>
        <h3>Author of the Book = <b>{{ book.author }}</b></h3>
        <br>
        <h3>Publish Year = <b>{{ book.year }}</b></h3>
        <br>
        <h3> Category = <b>{{ book.category }}</b></h3>
        <div style="padding: 10px 5px;">
            <div style="background-color: white;padding: 10px 30px; width:175px;border: 3px solid black;text-align:center;">
                <a  style="color:black; font-size:19px;" href="{% url 'adminbook' %}"><b>Go Back</b></a>
            </div>
        </div>
    </div>
{% endblock %}

inside of book_detail.html

{% block content %}
     {{ block.super }}
    <div style="margin:0 auto;width:1200px;">
        <h1 class="page-header" style="color:black;">{{ book.title }}</h1>

        <h2> This book is <b>{{ book.availability }}</b> to borrow</h2>
        <br>
        <h3>Author of the Book = <b>{{ book.author }}</b></h3>
        <br>
        <h3>Publish Year = <b>{{ book.year }}</b></h3>
        <br>
        <h3> Category = <b>{{ book.category }}</b></h3>
        <div style="padding: 10px 5px;">
            <div style="background-color: white;padding: 10px 30px; width:175px;border: 3px solid black;text-align:center;">
                <a  style="color:black; font-size:19px;" href="{% url 'book' %}"><b>Go Back</b></a>
            </div>
        </div>
{% endblock %}

inside of the url

urlpatterns = [
    path('request_book/', request_book, name='request_book'),
    re_path(r'book_detail/(?P<pk>[0-9]+)/', book_detail, name='book_detail'),
    re_path(r'admin_book_detail/(?P<pk>[0-9]+)/', admin_book_detail, name='admin_book_detail'),
    path('add_book/', add_book, name='add_book')

]

Edit : Ok I fixed it.

question from:https://stackoverflow.com/questions/65902465/django-template-takes-the-information-from-the-other-one

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

What I can see is that both of your views does the same thing. So it really won't matter whichever view you call.

It's different on the HTML page on basis of how you want to represent that information.

They both access the same object without any specific filter, so they're going to return the same object. If you are looking to display different filtered results, you need to change the query.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...