I want it to be changed to become a freelancer when we are in client page and vice-versa  when we are in freelancer page in django?

Submitted 3 years, 8 months ago
Ticket #55
Views 219
Language/Framework Django
Priority High
Status Closed

 This is my nav bar dropdown in django template. I want it to be changed to become a freelancer when we are in client page and vice-versa  when we are in freelancer page.

Submitted on Aug 02, 20
add a comment

3 Answers

@Swastikshrestha0001 , you can achieve this by if else logic. Lets say if the current user == 'client' then you can do something like this,

{% if user == client %}
     'Freelance'
{% else %}
    'Become a client'
{% endif %}

Something like you can try it out.

Submitted 3 years, 9 months ago

@Aruntck I know the logic of if else but as you say user== client it only applies when there is separate account for freelancer and client. So,I have an web app where i want to switch from freelancer to client and vice-versa from just single account.

- swastikshrestha0001 3 years, 9 months ago

Where you are storing the difference I mean you should have some model right to differentiate the 2 categories, based on that you can apply

- sunil 3 years, 9 months ago

@Swastikshrestha0001 can you confirm about the status?

- Vengat 3 years, 9 months ago

@sunil i made 2 categories on model but i did not get it how to use loop in views where i can switch to client and freelancer with just a click only with 1 account.

- swastikshrestha0001 3 years, 9 months ago

Can you share some code or articles ?

- swastikshrestha0001 3 years, 9 months ago

How about this approach. Let say, if you create the dynamic url to hold this as parameter 

/* Urls.py */
path(<str:param>,views.home,name='home')

/* views.py */


def home(request,param):
    if param == 'Freelancer':
        qs = Model.objects.filter(type=param)
        context = {
            'object_list': qs
        }
        return render(request,'home.html', context)
    elif param == 'client':
        qs = Model.objects.filter(type=param)
        context = {
            'object_list': qs
        }
        return render(request,'home.html', context)
    else:
        return 'soemthing else'
  

This is high level implementation . Now in your html template apply the filter and render it correctly.

Submitted 3 years, 9 months ago

@Aruntck i used your approach and i try to use filter in my html template but i am not sure this is the right way to use filter in html template correct me if i am wrong. I am really sorry for bad code format and I honestly dont know how to format code in Teckiy . I try to format it as mention ``` but it did not work.

* / home.html /*

{% for object in data.filtered %} <a class="dropdown-item" href="{{object}}">Become a Client</a>

{%endfor%}```

- swastikshrestha0001 3 years, 9 months ago

you can loop the queryset and apply the filter something like below, {% for obj in object_list % } {% if obj.type == 'client %} <a class="dropdown-item" href="{{object}}">Become a Client</a> {% else %} <a class="dropdown-item" href="{{object}}">Become a Freelancer</a> {% endif %} {% endfor %}

- Vengat 3 years, 9 months ago

@Aruntck I did as you say but its not showing me become a client nor become a freelancer in a nav bar.

- swastikshrestha0001 3 years, 8 months ago

can you share your views and html code in github or pastebin?. I will take a look.

- Vengat 3 years, 8 months ago

@Aruntck this is my pastebin link. Please have a look.

https://pastebin.com/yrN8GDiz

- swastikshrestha0001 3 years, 8 months ago

@Swastikshrestha0001 I am unable to open the link. May be its private page, you may need to provide access to public i guess.

- Vengat 3 years, 8 months ago

@Aruntck i am sorry about that now i have change it to public. please have a look.

here is the link :

https://pastebin.com/yrN8GDiz

- swastikshrestha0001 3 years, 8 months ago

<p>@Swastikshrestha0001 when you upload the details, please provide which view, url & template info?. You have uploaded everything ,I would like to know in which view,url you implemented which is not working. It would be great if you provide detail info about the code where you have issue .it will be easy for us to debug.</p>

- Vengat 3 years, 8 months ago

@Aruntck I uploaded view, url and template where i have a problem because i feel that is making a issue. Anyways i have again upload new one just to simplify way as you have said so here is the link : https://pastebin.com/M3GxGTgh

please have a look.

- swastikshrestha0001 3 years, 8 months ago

ok looks like issue in for loop in html,
```{% for object in object_list %} {% if object.type == 'client' %}

                <a class="dropdown-item" href="{{object}}">Become a Client</a>
                {% else %}
                <a class="dropdown-item" href="{{object}}">Become a Freelancer</a>
                {% endif %}

```
instead of obj.type you need to change to object.type. Try this and let me know your feedback.

- Vengat 3 years, 8 months ago

@Aruntck I changed obj.type to object.type , it didn`t fixed my issue. Any other suggestions?

- swastikshrestha0001 3 years, 8 months ago

When you say didnt fix what output you are getting, the same logic is working as expected in my end

- Vengat 3 years, 8 months ago

one more thing i just noticed, as per the code which i shared at any point of time it should return either "CLIENT/FREELANCER" queryset. In your template, you just need to check the param == Client then do the logic or do freelancer logic.

- Vengat 3 years, 8 months ago

@Aruntck after i change to object.type  "become freelancer or client" is not showing. umm.. I guess its not returning either client nor freelancer queryset.

Submitted 3 years, 8 months ago

ok lets add one more variable in the view respective queryset like below
def home(request,param): if param == 'Freelancer': qs = Model.objects.filter(type=param) context = { 'object_list': qs, 'type':'Freelance' } return render(request,'home.html', context) elif param == 'client': qs = Model.objects.filter(type=param) context = { 'object_list': qs, 'type':'client' } return render(request,'home.html', context) else: return 'soemthing else'
now in your template based on output type you can route the required URL.

- Vengat 3 years, 8 months ago

As we didn't get any response on this. We are closing the ticket.

- Vengat 3 years, 8 months ago


Latest Blogs