Disjunction between two types of users using if

Asked

Viewed 52 times

0

Opa, I’m using djando admin for login, by Accounts, and I’m using the User extended to two different tables, Business and User, and I’m doing fully signed up for user.negocio and user.usuario. So far so good, but when I log into the system I have a menu that I use via Inlcude and in it appear some user data, name, photo and more. In the code below I show how I tried the logic of for user.negocio show data of the respective user if it is user.user the same. But what I noticed is that {% if user.is_authenticated %} is validating and only it, because if I put in the first if user.usuario.name and user.negocio.name and then with user, it shows the name, even if it is within the user’s if.negocio And I’m not using any views for the.html menu. And if I take Elif and let it show in the menu both names and photos, when soon with user appear their data and when soon with business tbm, I think the problem is in Elif

html menu.

{% if user.is_authenticated %} 
   {% if user.negocio %}
     {{user.negocio.empresa}} 
     <img src="{{ user.negocio.foto.thumbnail.url}}">
{% elif user.usuario %}
     {{user.usuario.nome}}
     <img src="{{ user.usuario.foto.thumbnail.url}}" >
{% endif %} 
   {% else %}
       <form class="form-inline">
        <a class="btn btn-primary btn-sm" href="{% url 'login' %}"> Entrar </a>
       </div>
       </form>
{% endif %}

1 answer

0

I didn’t quite understand your question. I don’t know if a User has necessarily both User and Business or if it can or can’t be a Business. For it differs in such cases.

Whether a User can only be or Usuario or Negocio, you should set one of them to null. That way you can do if

{% if user.negocio %}
     {{user.negocio.empresa}} 
     <img src="{{ user.negocio.foto.thumbnail.url}}">
{% elif user.usuario %}
     {{user.usuario.nome}}
     <img src="{{ user.usuario.foto.thumbnail.url}}" >
{% endif %} 

If a User can be both User and Business, you should control through the context for example, in which way you are for example

{% if modo_negocio %}
     {{user.negocio.empresa}} 
     <img src="{{ user.negocio.foto.thumbnail.url}}">
{% else %}
     {{user.usuario.nome}}
     <img src="{{ user.usuario.foto.thumbnail.url}}" >
{% endif %} 

From what I understand, the user can contain User and Business in your code. If you did not put elif, he would enter the two ifs as you noticed and would show both photos (as none of them is null)

  • Hello friend, yes user can be both user, as business, I am at work now, but half day I will try this your code, the last one sent, I am very new, as I use the context?

  • and you are right both User and Business inherit from User, and the two are deferent tables I want to use both in the menu, if the user.user log in the data, as if log in user.negocio their data in the menu and even I want to use this system in the profile part meusite.com.br/profile shows user.user or user.negocio all within a profile.html only

  • Context is like variables that are passed from the view to the template, a Python dictionary. You would have to go through the view or you can create a Processor context to be passed in all views. I suppose you start at first to understand the flow

  • I will try to assemble this context in my view, as it is passed to menu.html and this is include within all pages, I believe doing just for it serveria right

  • You can use Session too

  • and how to pass these two context skin view options?

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.