O
O
outsider_x2019-05-04 01:54:05
Django
outsider_x, 2019-05-04 01:54:05

Where to find views for base template in Django?

There is a piece of code that needs to be pushed into the Django template. I tried it in different ways, but it does not push. Please advise how to proceed:

{% Profile.objects.get(name__exact=request.user.get_username).get_absolute_url %}

After a little googling, I came to the conclusion that it is better to do this separately in views.py. But this piece needs to be inserted into the most basic template. So the second question is how to use views for base templates?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
7
776166, 2019-05-06
@outsider_x

0) To base, not to base (inheriting base), it doesn't matter. What matters is how exactly to pass it to the template. No context is passed to the base template. The base template simply extends your template, and there is only one context.
1) In your case, you MUST use the regular request.user and don't mess with your brain ( https://docs.djangoproject.com/en/2.2/ref/request-...
There will be something like {{ request.user.profile.get_absolute_url }}
UPD: I didn't see it right away. You have a separate model for the profile, bound to the user.You
need to make a property in the model that will give the profile for this user.If you can, then the issue is resolved and will work as in the example above.If not, the easiest way is to add a context processor and give profile variable.It will work something like this:{{ profile.get_absolute_url }}. But it is best, of course, to define the profile property in the User model.
2) If the regular request.user is not suitable (why!?), or you want to understand the principle for the future, then there are two similar options:
2.1) Pass your object to the context of a specific template from the view. Will only work in this view. Banal, right?
2.2) Write a context processor and add it to settings.TEMPLATES.OPTIONS.context_processors (see how it's done in the base settings.py and google for examples). It will work generally everywhere within the framework of rendering templates. We are not looking for easy ways for request.user!
Vadim Shatalovsuggests using filters/tags. This is not quite right (not quite about this), because they will still need to either pass request.user, or take it from the context, where it will have to be placed in one of the ways described above, or in some inhuman way from somewhere get what you don't need to do.

V
Vladimir, 2019-05-04
@vintello

as long as there are words "need to push" and "he does not push" you need to go here . there is the whole truth about the pieces of code and how to cram them and actually where

V
Vadim Shatalov, 2019-05-04
@netpastor

Look aside https://docs.djangoproject.com/en/2.2/howto/custom...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question