2

I am new to django, and I see that you could create templates that you could populate in views. You could also create some basic.htm that everyone extends...

Let say I have two pages: (django demo)

  • List of all questions
  • Detail of the question.

Now I would like to create "one page" that have first view as sidebar and another as a "detail- right" view.

I would like that on clicking on the list in sidebar change right vies.

It would be nice, if I could use different views (inside views.py) for loading separate templates.

I wish for base html would be something like this :

<head>
 <meta charset="UTF-8">
 <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
 <title>Makro Zadravec</title>
 {% load staticfiles %}
 <link rel="stylesheet" type="text/css" href="{% static 'demo/css/bootstrap.min.css.css' %}" />
</head>
<body class="body" style="background-color: #f6f6f6">
 <div>
 <h1> This is title</h1>
 </div>
 <div>
 {% block sidebar %}
 {% endblock %}
 </div>
 <div>
 {% block content %}
 {% endblock %}
 </div>
</body>
</html> 

And then I would have template for blocks:

  • content
  • sidebar

in separate views.

asked Apr 3, 2018 at 7:20

2 Answers 2

1

Apart from styling here is the logic you can follow this

as you said you already have template which already loaded list of question then your base view would return all the question object as queryset.

First of all, you don't need to write separate template because you can handle this

{% block content %}
 // this will load list of all question
{% endblock %}
{% block detail %}
 // here display detail of question
{% endblock %}

create two urls to hit one without kwargs and other with kwargs (if you use django version < 2.0 you need to use url instead of path)

path('target/', QuestionView.as_view(), name='target-list'),
path('target/<int:pk>', QuestionView.as_view(), name='target-detail')

Now in view file you just need to handle the kwargs:

class QuestionView(TemplateView):
 template_name = "template.html"
 model = Question
 def get_context_data(self, **kwargs):
 context = super(QuestionView, self).get_context_data(**kwargs)
 context['question_list'] = #your queryset to list question#
 pk = kwargs.get('pk', None) # this will return pk (id of question) if hit url otherwise None
 if pk:
 context['question_detail'] = #your queryset to get question-detial# 
 return context

so if you hit url with kwargs it will return both list of question and detail with it as context in template which you can access as below:

{% block content %}
 # access question_list to list out all question
{% endblock %}
{% block detial %}
 {% if question_detail %} // if question_detail context passed from view
 # add detail of question by accessing question_detail
 {% endif %}
{% endblock %}
answered Apr 3, 2018 at 7:42

2 Comments

Thank you for reply. I understand your logic, but because this is a learning tutorial, I would like solution, where I have maybe different objects, (not related), that I need in multiple spaces. I prefer to to have separate views calls witch populate it's container, then have multiple copies (for example one view with Questions and Answers, another view with Answers and Statistics, another view with Questions and Statistic,.. hypothetically) I would rather have 3 views that return each object, and then populate specific containers on page. Is this possible?
in my code-logic I have used kwargs with same view but instead you can map it to another view and you'll get question detail from other view
0

You can do this based on one view.

Create variable called 'question' in your view which at start is defined as an empty string. Then if someone click on some question on sidebar block you can make url with parameter with id of question to show details ('/page/?question=1') or you can store this id in session, as you want. And call same view another time. Then in view you check if you get parameter in url or if it's stored in session and if it's true you assign question object (which you get by id) to variable 'question'. Send this variable to template. In template you render sidebar always and check if variable 'question' is not equal to empty string then you render details for question.

Something like this:

{% block sidebar %}
...
{% endblock %}
{% if question != '' %}
{% block content %}
...
{% enblock %}
{% endif %}
answered Apr 3, 2018 at 7:40

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.