1👍
Without seeing the code for the new_report
view, it looks like it must be loading the correct report info based on session.session['report_id']
. I think rather than passing things like that around via the session, you should probably just pass it as an argument to the new_report
view when you call redirect (see link for django docs on redirect). Note that you’d also have to edit your view definition doing this.
Better yet, combine edit_report
and new_report
into a single view, if you’ve got almost all of the edit functionality in edit_report
already.
Anyway, back to the question being asked. You can check if the report_id is set in you session in the new_report
view (I assume you’re doing this already). Then based on this, you can pass a variable in a RequestContext
to your template
Views.py
def new_report(request):
view_template = loader.get_template('template.html')
if 'report_id' in request.session:
highlight_new_report = True
else:
highlight_new_report = False
view_context = RequestContext(request,{
'highlight_new_report' : highlight_new_report
})
return HttpResponse(view_template.render(view_context))
Then wherever the menu is in your template (note that this could very likely be in a base template being extended by your_template.html
), you can use this to decide what class to add to the tab/link. There is probably something there already adding a class to highlight it, you should be able to override this based on the highlight_new_report
variable which is now available in your template
template.html (or a template template.html extends)
<!-- Menu tabs -->
<a href="/new_report"
{% if highlight_new_report %}
class="highlighted_tab">
{% endif %}
</a>
Edit for more info
Sorry should have been clearer. The above code is an example of how you can set the class based on your report_id
, but I left it unspecific because I didn’t know what your template would look like.
If you’ve used the above code to add a CSS class which removes the highlight, then you can add another class which will add a highlight:
template.html (or a template template.html extends)
<!-- Menu tabs -->
<a href="/new_report"
{% if highlight_new_report %}
class="highlighted_tab"
{% else %}
class="non_highlighted_tab">
{% endif %}
</a>
Where highlighted_tab
is a class which adds a highlight, and non_highlighted_tab
is one which doesn’t, or which overrides a highlight styling already being applied.
The reason I didn’t write the answer this way to begin with, is because you probably already had something like class="active"
being added based on the current page. So I expected you to use a condition like that above, to restrict the addition of this active
class to when highlight_report = True
1👍
It is possible to do using session.
By seeing this bit of code,i understand that you are creating a session for new report.But what you are trying to do is not possible to handle with the same session.
I think,for new report and edit report their are two different methods.If this is the case its easy to handle.
Let us see this,
1.You should create a session in edit report method.
2.Should validate,whether that session is in new report,if that newly created session is in new report should set new_report=False and pass to render.
3.You should delete that newly created session in new report method,if not then menu will always be not highlighted.
As per the above one,if the user clicks report from {% url incident.views.edit_report list.report.id%}
or edit report menu,the newly created session gets started from edit report method and will be available.
Hope this gives you an idea.
- [Answered ]-Django not reflecting changes to urls.py
- [Answered ]-Django, MySQL and regex: not supporting re syntax?
- [Answered ]-How to escape quotes when setting a javascript string to the value propagated from a Django variable?
- [Answered ]-Django: Setting initial vals of multiplechoicefield only works first time