I am creating a web app where users are allowed to post stuffs and i have a profile section where i want users to see the stuffs they have posted so i am using IF loop in django.
{% if user_post.author == "request.user.username" %}
hello there {{user_post.title}}
{% else %}
againnn
{{user_post.author}}
{{request.user.username}}
{% endif%}
here in the if loop i am checking for all the authors and comparing them with the user logged in.
but it is not executing, in the else part i am confirming names of the user . but the else part is executing.
i am not sure if it can be done this way..if not please suggest me a way
thanks
how about change the codes.
{% if user_post.author == "request.user.username" %}
to
{% if user_post.author == request.user.username %}
Related
I have a function which generate a pdf.file and send it by email. And it works perfect. And I have a Table on my frontend
like above.
In my Django Model - Point 1 set by default as False, by condition if Point 1 is False - Cell 2 is empty, else - marked as Done. When I changing Table via Django form it works fine as well (frontend marked as Done). The problem is when I trying to change this via function which generate a pdf. I have added below lines of code in my pdf.generate Function:
def generatePdf(request, pk):
point = get_object_or_404(MyObj.objects.select_related('related'), pk=pk)
...
email.send(fail_silently=False)
point.one = True
print(point.one)
messages.success(request, 'Success')
return HttpResponseRedirect....
at the terminal I got the message that value changed properly from False to True
but for some reason Cell 2 in my Table on the frontend still is empty...
Part of frontend code:
{% for item in object_list %}
...
<td>
{% if item.one %}
<span><i class="fa fa-solid fa-check"></i></span>
{% else %}
<span></span>
{% endif %}
</td>
...
{% endfor %}
Summarizing the above said - why frontend condition working properly only if I change via Django form (function) and not if I trying to change via generatePdf function? Cell value in database changed properly in both ways!
I just forgot to add:
point.save()
in order to save to database changes.
So simple...
I am trying to make my navbar element active if the user is on the current url and any url's leading from it.
For example, I want the navbar to be active on:
http://example.com/products
AND
http://example.com/products/discounted-items
I was using this:
{% if url_name == 'products' %}active{% endif %}
and was very happy till I realised that once I progress from the 'products' page to 'products/discounted-items' it would cease being active.
Is there a clean way to do this in django?
Thank you very much.
In your case you could simply do the following:
{% if 'products' in url_name %}active{% endif %}
Be aware that this also causes /productsfoobar to be active.
To prevent that you could use products/ instead of checking it without the trailing-slash.
In case (as i seen the page) if u are using Bootstrap u can add "active" in the class field in nav
The first step of Django two-factor auth is just a page where user is asked to click on "activate two factor authentication".
How can I skip that step wizard.steps.current == 'welcome' and get the user directly to the actual page where he chooses his authentication method. wizard.steps.current == 'method'.
{% if wizard.steps.current == 'welcome' %}
...
{% elif wizard.steps.current == 'method' %}
...
{% elif ... %}
...
{% endif %}
A possible solution is to override the wizard.step.current by setting it up to 'method' before user hit the setup.html page but I don't know to achieve this.
Thanks
The solution was quite easy.
In the core.py of the two-factor package find the class SetupView() and comment or remove the line ('welcome', Form)
in the view that preceed the two factor setup insert the following code:
return redirect('/account/two_factor/setup/').
This will redirect directly the user to setup page and skip two-factor explanatory page
I am currently working on a developing a web application that people can use to browse through book reviews, but if they login they can post comments on the book reviews, or post their own reviews of books.
While I have implemented posting new reviews. I am do not understand how to modify the method in my views.py file that renders a book review such that if the user is logged in, in shows all previous comments, a form for the user to post a comment and a logout button, and if the user is not logged in it simply shows the book review and a log in button.
You can tell if your user is logged in or not in your views like this:
if request.user.is_authenticated():
# logged in logic
return render_to_response("loggedin_templates.html")
else:
# not logged in logic
return render_to_response("not_loggedin_templates.html")
Or you can tell them in the templates like this:
{% if user.is_authenticated %}
<span>{{ user.username }}</span>
{% else %}
<li>login
{% endif %}
I was wondering if I can get django admin (logged as super user) to display in some kind of "recent actions box", changes other users (non super users) made?
Thanx,
Luka
LogEntry.objects.log_action()
http://www.djangosnippets.org/snippets/1052/
Tying in to Django Admin's Model History
it was my problem too, and it has a simple answer
Just go this address and do the following;
Address: your virtulenv/lib/site-packages/django/contrib/admin/templates/admin
And find base.html, inside of this file edit after {% load Log %} and simply add this :
{% if user.is_superuser %}
{% get _admin_log (numbers of actions you want) as admin_log %}
And for the else do the same but at the end don't forget to add for_user user 😊