How to use the Django Usurena "mugshot" template variable - django

I'm trying to use Userena in our Django website, but I can't seem to figure out how to use the template tag to display the mugshot. I have tried the following to spit out the URL within an image tag:
<img src="{{ owner_profile.get_mugshot_url }}">
and
<img src="{{ profile.get_mugshot_url }}">
Anyone have some insight??
Thanks!

based on alican answer, just put following code in your template:
<img src="{{ user.get_profile.get_mugshot_url }}" />

Use the following code to display Userena profile image(mugshot) in your template. Use appropriate username to filter the required user.
views.py
from django.shortcuts import get_object_or_404
from django.contrib.auth.models import User
def my_view(request):
profile = get_object_or_404(User, username__iexact=username).get_profile()
return render_to_response('template.html', locals(), context_instance=RequestContext(request))
Here I have rendered this variable "profile" to the template "template.html". Include following code in your template to display mugshot image.
template.html
<img src="{{ profile.get_mugshot_url }}" />
It worked for me. Hope it will work for you too. Thanks.

Try this:
{{ user.get_profile.get_mugshot_url }}

Here's the way it worked for me:
{{ user.get_profile.get_mugshot_url }}
But make sure you use render as opposed to render_to_response for each of the pages that you'll be pulling it in (ex: views.py):
from django.shortcuts import render
return render(request, 'sometemplate.html', {"name": "some_var"}, )
Here's how I did it, pulling in the mugshot for the navbar dropdown (ex: sometemplate.html):
<ul class="nav pull-right">
{% if user.is_authenticated %}
<li class="dropdown">
<a href="#" class="dropdown-toggle user-dropdown" data-toggle="dropdown">
<img class="user-thumbnail img-circle" src="{{ user.get_profile.get_mugshot_url }}" alt="" />
Hi, {{ user.username }}
<b class="caret"></b></a>
<ul class="dropdown-menu">
<li><i class="icon-wrench"></i> Profile</li>
<li class="divider"></li>
<li><i class="icon-off"></i> Log Out</li>
</ul>
</li>
{% else %}
<li>Log in</li>
{% endif %}
</ul>

Related

How do I upload Image using Bootstrap Card and Django for loop?

I am trying to upload image. I used Django for loop and inside it placed Bootstrap Card.
I have added the code of models.py and views.py:
The problem I am facing is that the images are not appearing on the browser while rest of the details such as name and price is loading perfecting.
Products
{% for product in products %}
<div class="row">
<div class="column">
<div class="card" style="width: 18rem;">
<img src="{{product.image_url}}" class="card-img-top" alt="...">
<div class="card-body">
<h5 class="card-title">{{product.name}}</h5>
<p class="card-text">${{product.price}}</p>
ORDER NOW
</div>
</div>
</div>
</div>
<ul>
{% endfor %}
`enter code here:
models.py
from django.db import models
class zoho(models.Model):
name= models.CharField(max_length=255)
price=models.FloatField()
stock=models.IntegerField()
imageurl=models.CharField(max_length=300)
`
enter code here
views.py
from django.http import HttpResponse
from django.shortcuts import render
from .models import zoho
def index(request):
products= zoho.objects.all()
return render(request, 'index.html', {'products': products})
{% for product in products %}
.....
<img src="{{product.image.url}}" alt="Product Image" class="img-fluid">
.......
......
{% endfor %}
<form enctype="multipart/form-data" method="POST">
{% csrf_token %}
{{form | crispy}}
<input type="submit" class="btn btn-success btn-sm mr-3 mb-2 px-3 py-1" value="Save" />
.....
</form>
image_url = models.ImageField(upload_to ='upload_profile_to')enter code here
In your models.py, you're saving the image link to imageurl and not image.url as you're currently calling it in the for loop block.
So, correct the way you called the variable in the for loop block

Active nav-item highlighting based on pk passed in url

I have 4 nav-items being generating dynamically based on the Django Model entries.
My template code is as below:
{% for stockbroker in stockbrokers %}
<li class="nav-item">
<a class="nav-link" href="{% url 'broker:display_stocks' stockbroker.id %}" id="nav">{{ stockbroker.broker_name }}
</a>
</li>
{% endfor %}
I want to highlight the currently active nav based on the id I am passing in the url section of the a tag href. Is it possible to achieve this?
I am generating these nav links in the base.html using context_processors.py
from .models import StockBroker
def stockbrokers(request):
return {'stockbrokers': StockBroker.objects.all()}
An if condition should work for this:
{% for stockbroker in stockbrokers %}
<li class="nav-item">
<a class="nav-link {% if stockbroker.id == current_id %}active{% endif %}" href="{% url 'broker:display_stocks' stockbroker.id %}" id="nav">{{ stockbroker.broker_name }}
</a>
</li>
{% endfor %}
Note: current_id (or variable name of your preference) should be passed in the context.
The context is the dictionary that is passed while rendering the template, example:
def my_view(request):
# View Code
return render(request, 'template_name.html', {'current_id': current_id})

How To Fix Auto Login In Django?

How To Fix Auto login When I go to this URL like this: 127.0.0.1:8000/profile/1 When I see If Someone go to that URL so Django login him without password and username
ERROR:
How To Fix Auto Login In Django
You Can See The Gif For More Details:
Views.py
def profile_detail(request,pk):
user = get_object_or_404(User, pk=pk)
model = user_register_model()
return render(request,'profile_detail_view.html',{'user':user,'model':model,})
urls.py
from . import views
from django.urls import path
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('', views.index,name='index'),
path('accounts/signup/', views.user_reg,name='register'),
path('profile/<int:pk>',views.profile_detail,name='profile'),
]
Here is my Base.html
<ul class="navbar-nav ml-auto">
{% if not user.is_authenticated %}
<li class="nav-item">
<a class="nav-link navaour" href="{% url 'register' %}"><i class="fa fa-check-square-o"></i> Sign up Free</a>
</li>
<li class="nav-item">
<a class="nav-link navaour" href="{% url 'login' %}"><i class="fa fa-user"></i> Login</a>
</li>
{% else %}
<li class="nav-item">
<a class="nav-link navaour" href=""><i class="fa fa-user"></i> Profile</a>
</li>
<li class="nav-item">
<a class="nav-link navaour" href="{% url 'logout' %}"><i class="fa fa-power-off"></i> Logout</a>
</li>
{% endif %}
Here is my profile_detail_view.html
<div class="row">
<div class="col-sm-3 col-md-2 col-5">
<label style="font-weight:bold;">Full Name</label>
</div>
<div class="col-md-8 col-6">
{{user.username}}
</div>
</div>
Any Help Appreciated
Thanks!
replace {% if not user.is_authenticated %} with {% if not request.user.is_authenticated %} in your template. You missed request.
you can also use #login_required before the def in views.py and importing library
from django.contrib.auth.decorators import login_required
This will help in blocking those views of url for the users who aren't logged in.

Django - Change active navbar template based on webpage

I have a template that looks like this in my html. It uses the bootstrap classes.
<-- navbar-template.html>
<div class="collapse navbar-collapse" id="myNavbar">
<ul class="nav navbar-nav">
<li>Home</li>
<li class='active'>Members</li>
<li>Research</li>
<li>Publications</li>
<li>Links</li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li><span class="glyphicon glyphicon-log-in"></span> Login</li>
</ul>
</div>
I like the active class but I need the to change which list object it is based on which page from the navbar django has loaded.
I think you'd like to do something like this in the home.html file
<-- HOME -->
{% include "navbar_template.html" with page="home"} %}
###Do something with {{page}} variable in order to set the home list tag to active.
Do I have to write a crazy amount of if else statements or is there an easier way. Perhaps something with the views.py in django
You can do it like this (example solution I use on my page):
<ul class="sidebar-nav--primary">
{% url 'main:home' as home %}
{% url 'main:work' as work %}
{% url 'main:personal' as personal %}
<li><a href="{{ home }}" {% if request.path == home %}class="active"{% endif %}>Home</a></li>
<li><a href="{{ work }}" {% if request.path == work %}class="active"{% endif %}>Work</a></li>
<li><a href="{{ personal }}" {% if request.path == personal %}class="active"{% endif %}>Personal</a></li>
</ul>
A cleaner method would be creating a custom template tag. Something like is_active:
# Inside custom tag - is_active.py
from django.template import Library
from django.core.urlresolvers import reverse
register = Library()
#register.simple_tag
def is_active(request, url):
# Main idea is to check if the url and the current path is a match
if request.path in reverse(url):
return "active"
return ""
And use it in your templates like this:
# template.html
{% load is_active %}
<li>Home</li>

Issue with views on button click

I am trying to make a carousel that you can upload pictures to. However, when I click on the upload button, It switches to another page, rather than giving me just the upload selection panel as Intended. Can anyone tell me why this might be?
Views.py
from django.shortcuts import render
from django.shortcuts import render, redirect
from django.contrib.auth import authenticate, login
from webportal.views.authentication import LoginForm
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse
from django.conf import settings
from webportal.forms.forms import DocumentForm
from webportal.models import Document
is_server = True
def list(request):
# Handle file upload
if request.method == 'POST':
form = DocumentForm(request.POST, request.FILES)
if form.is_valid():
newdoc = Document(docfile = request.FILES['docfile'])
newdoc.save()
# Redirect to the document list after POST
return HttpResponseRedirect(reverse('webportal.views.list'))
else:
form = DocumentForm() # A empty, unbound form
# Load documents for the list page
documents = Document.objects.all()
#documents=DocumentForm().
# Render list page with the documents and the form
return render_to_response(
'webportal/carousel.html',
{'documents': documents, 'form': form,},
context_instance=RequestContext(request)
)
Carousel.html
{% load staticfiles %}
{% load filename %}
<div class="container">
<div class="row">
<div class="col-md-12">
<div id="myCarousel" class="carousel slide" data-ride="carousel">
<!-- Indicators -->
<ol class="carousel-indicators">
<li data-target="#myCarousel" data-slide-to="0" class="active"></li>
<li data-target="#myCarousel" data-slide-to="1"></li>
<li data-target="#myCarousel" data-slide-to="2"></li>
</ol>
<div class="carousel-inner" role="listbox">
{% for document in documents %}
<div class="item {% if forloop.first %} active {% endif %}">
<div class="row">
<div class="col">
<img src = "{{STATIC_URL}}img" >
</div>
</div>
</div>
{% endfor %}
</div>
<a class="left carousel-control" href="#myCarousel" role="button" data-slide="prev">
<span class="glyphicon glyphicon-chevron-left"></span>
<span class="sr-only">Previous</span>
</a>
<a class="right carousel-control" href="#myCarousel" role="button" data-slide="next">
<span class="glyphicon glyphicon-chevron-right"></span>
<span class="sr-only">Next</span>
</a>
</div>
<!-- /.carousel -->
</div>
</div>
<form action="{% url 'webportal:list' %}" method="post" enctype="multipart/form-data">
{% csrf_token %}
<p>{{ form.non_field_errors }}</p>
<p>{{ form.docfile.label_tag }} {{ form.docfile.help_text }}</p>
<p>
{{ form.docfile.errors }}
{{ form.docfile }}
</p>
<p><input type="submit" value="Upload" /></p>
</form>
</div>
Forms:
class DocumentForm(forms.Form):
docfile = forms.ImageField(label='Select a file', help_text='max. 42 megabytes')
Models:
class Document(models.Model):
docfile = models.ImageField(upload_to='webportal/static/img/')
I'm not sure what you mean by "just the upload selection panel as Intended".
Firstly, I see a problem by you returning an empty form if the submitted form is invalid, you should rather return the existing form and show the errors.
Your form, upon clicking the upload button will send a POST request to your server and your view generates a whole new HTML page as a response. HTTP is stateless, meaning that whatever your browser showed before is basically no longer of relevance. So naturally you will see a new page.