Models.py
solution_req = models.CharField(max_length=250, default=0, choices=(('FLC', 'FLC'), ('FSC', 'FSC'),
('Crate', 'Crate'), ('PPBox', 'PP Box')))
HTML
{% ifequal l.solution_req PPBox %}
Create Solution
{% else %}
<button class="btn btn-primary" disabled>
Create Solution
</button>
{% endifequal %}
The above are my models and django templates
The problem is when the object has solution_req as PPBox it is still showing the disabled button instead of the link
Related
I'm creating a Truth System for my project PoopFacts, where people can vote for True, False, or remove their vote.
I have the voting system working, but
I'm trying to make the button change colors to show if they've voted for True of False already or uncasted their vote.
I'm showing the "Like button" code, cause it's simpler and uses the same logic.
.models
PoopFact(models.Model):
likes = models.ManyToManyField(User, related_name='likes')
.views
def home(request):
poopfacts = PoopFact.objects.all().order_by('-date')
context = {'form': form, 'poopfacts':poopfacts,}
return render(request, 'home.html', context)
The idea is pretty much something like this
html
{% for poopfact in poopfacts %}
{% if poopfact.likes.user.exists() %}
<button type="submit" class="btn btn-primary btn-block">Like</button>
{% else %}
<button type="submit" class="btn btn-block">Like</button>
{% endif %}
So if they've liked it, the button will be blue, and if they click it again it will uncast their vote and make it normal.
Does anybody have a good idea to make this work? I've been trying so many things with no luck.
in html
{% for poopfact in poopfacts %}
{% if user in poopfact.likes.all %}
<button type="submit" class="btn btn-primary btn-block">Like</button>
{% else %}
<button type="submit" class="btn btn-block">Like</button>
{% endif %}
{% endfor %}
this might seem silly, but i need help,
I have a model, in which if "is_vendor" is True, I want it to display something, while if "is_vendor" is False, I dont want the item to display. I already figured how to switch the is_vendor from True to False or vice versa, What i want now is to know how to complete {% if user_profile.is vendor... statement (Plus Im not sure if want i typed there is close to correct. Thank you
Model:
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
bio = models.CharField(max_length=245, null=True)
image = models.ImageField(default='default.png', upload_to='profile_pics')
is_vendor = models.BooleanField(default=True)
My template:
**{% if user_profile.is_vendor**
<div style="margin-left: 40px">
<a class="btn btn-sm btn-outline-primary mb-4 mr-3 "href="{% url 'vendor_register' %}">
Register
</a>
</div>
{% if user_profile.isVendor %}
<div style="margin-left: 40px">
<a class="btn btn-sm btn-outline-primary mb-4 mr-3 "href="{% url 'vendor_register' %}">
Register
</a>
</div>
{% endif %}
{% if user_profile.isVendor %}
your code in here
{%endif %}
If the user is logged in, you can access that user with {{ user }}. Since you have a OneToOneField to the Profile model, you thus can access the Profile record of the logged in user with {{ user.profile }}, and thus check if it is a vendor with {{ user.profile.is_vendor }}, we thus can wrap this in an {% if … %} … {% endif %} template tag [Django-doc]:
{% if user.profile.is_vendor %}
…
{% endif %}
I'm trying to return JsonResponse in Django View:
return JsonResponse(render_to_string(
'notifications/notifications_dropdown.html',
{'new_notifications': new_notifications}),
safe=False)
notifications_dropdown.html:
{% if new_notifications %}
{% for notification in new_notifications %}
<a class="dropdown-item" href="{{ notification.task.get_absolute_url }}">
<span class="text-success">
<strong><i class="fas fa-info fa-fw"></i>{{ notification.title }}</strong>
</span>
<span class="small float-right text-muted">{{ notification.created|date:"SHORT_DATETIME_FORMAT" }}</span>
<div class="dropdown-message small">{{ notification.text }}</div>
</a>
<div class="dropdown-divider"></div>
{% endfor %}
{% endif %}
And my Notification Model has ForeignKey:
task = models.ForeignKey(Task, on_delete=models.SET_NULL,
null=True)
The problem is that {{ notification.task.get_absolute_url }} returns nothing. But when I get the same notification object in shell, it returns correct url. Besides, I use {{ task.get_absolute_url }} in other templates, and it works as expected.
Any ideas, why {{ notification.task.get_absolute_url }} doesn't work in template?
Thanks in advance.
I had
new_notifications = Notification.objects.filter(
user=request.user,
status=Notification.NEW
).values()
And .values() didn't include task object. It just returned task_id
I have the following form displaying entries of a model for user settings. When selected, I would like that a button catches its pk and send it to a Delete view.Here is the current code but I am missing this part.
user_detail template html
<form id="SettingsListForm"><label>  Settings List :      </label>
{% if user.usersetting.first %}
<select class="form-control" name="settingslist" id = "settingslist" form="SettingsListForm" >
{% for settings in user.usersetting.all %}
<option value="{{ settings.file.url }}">{{ settings }}
</option>
{% endfor %}
</select>
{% else %}
<li class="list-group-item">NO SETTINGS YET</li>
{% endif %}
<button class="btn btn-outline-light btn-circle"><i class="glyphicon glyphicon-minus" href="{% url 'my_app:setting_delete' pk=user.usersetting.last.id %}"></i></button>
{% block delete_setting_confirm_block %}
{% endblock %}
</form>
setting_confirm_delete html template with delete_setting_confirm_block
{% extends 'login_app/user_detail.html' %}
{% block delete_setting_confirm_block %}
<h4>
ARE YOU <b>REALLY</b> SURE YOU WANT TO <b>DELETE</b> THIS SETTING ?
<form method="POST">
{% csrf_token %}
<button type="submit" class="btn btn-outline-light btn-danger" value="Delete">YES</button>
<a class="btn btn-outline-light btn-default" href="{% url 'login_app:user_detail' pk=user.id %}"><b>NO</b></a>
</form>
</h4>
{% endblock %}
my_app urls
url(r'^setting/(?P<pk>\d+)/$',views.UserSettingDeleteView.as_view(),name='setting_delete'),
UserSettingDeleteView in my_app views
class UserSettingDeleteView(DeleteView):
model = models.UserSetting
template_name = 'my_app/setting_confirm_delete.html'
def get_success_url(self):
return reverse('my_app:user_detail', kwargs={'pk': self.object.user.pk})
Somehow, a similar technique works fine when using listgroups:
working sample in user_detail html
<ul class="list-group">
{% if user.userdata.first %}
{% for data in user.userdata.all %}
<li class="list-group-item">{{ data }}<a class="btn btn-outline-light btn-circle" href="{% url 'my_app:data_delete' pk=data.pk %}"><i class="glyphicon glyphicon-remove"></i></a></i></li>
{% endfor %}
{% block delete_data_confirm_block %}
{% endblock %}
{% else %}
<li class="list-group-item">NOTHING RECORDED YET</li>
{% endif %}
</ul>
In your template.html, you should create a form for deletion, like this:
<form action="{% url 'my_app:setting_delete' pk=user.usersetting.last.id %}" method="post">
{% csrf_token %}
<input type="submit" value="Delete" class="btn btn-outline-light btn-circle">
</form>
Because in HTML you can not send directly PUT or DELETE, you should fake it via a POST request. It will be useful to read this, it is explained well.
Your UserSettingDeleteView can be as simple as that:
from django.views.generic.edit import DeleteView
from django.urls import reverse_lazy
class UserSettingDeleteView(DeleteView):
model = MyModel
# Replace with the model you want to delete (User)
success_url = reverse_lazy('my_app:setting_list')
# After deletion, possibly you will want to redirect the user to the list view
The built-in DeleteView cares itself to find your model by the pk parameter you pass through your url and delete it, you just have to configure the model and success_url fields.
Now if you click on your Delete button, you should expect your desired entry to be deleted and the user to be redirected to the list view.
EDIT:
I forgot that you want to get this entry via options. Here you will have to use some JavaScript in order to find the currently selected element and send it's pk to your DeleteView.
At first, add id's to your option tags, like this:
<select class="form-control" name="settingslist" id="settingslist" form="SettingsListForm">
{% for settings in user.usersetting.all %}
<option value="{{ settings.file.url }}" id="{{ settings.id }}">{{ settings }}</option>
{% endfor %}
</select>
And then add some jQuery:
var settingId = $('#SettingsListName').find(":selected").attr('id');
In the end, you need to send settingId to the corresponding url, but I am not very familiar with JavaScript.
Try putting a hidden input in your form. Right now, nothing is passing through the form.
<input type="hidden" name="del_setting" value="{{user.usersetting.last.id}}">
So your form would look like this
<form method="POST">
{% csrf_token %}
<input type="hidden" name="del_setting" value="{{user.usersetting.last.id}}">
<button type="submit" class="btn btn-outline-light btn-danger" value="Delete">YES</button>
<a class="btn btn-outline-light btn-default" href="{% url 'login_app:user_detail' pk=user.id %}"><b>NO</b></a>
</form>
You also probably should move this out of the current form in your user detail template so that you're not putting a form within a form:
{% block delete_setting_confirm_block %}
{% endblock %}
None of the previously mentioned solutions would work so I guess the problem is the nested bootstrap select item which is supposed to display the pk of the setting.
For simplicity I then removed the problem by using list-group instead
<ul class="list-group">
{% if user.usersetting.first %}
{% for settings in user.usersetting.all %}
<li class="list-group-item">{{ settings }}<a class="btn btn-outline-light btn-circle" href="{% url 'my_app:setting_delete' pk=settings.pk %}"><i class="glyphicon glyphicon-remove"></i></a></li>
{% endfor %}
<label>Create a new setting...</label>
<a class="btn btn-outline-light btn-circle"><i class="glyphicon glyphicon-plus"></i></a>
{% else %}
<li class="list-group-item">Create a new setting...<a class="btn btn-outline-light btn-circle"><i class="glyphicon glyphicon-plus"></i></a></li>
{% endif %}
{% block delete_setting_confirm_block %}
{% endblock %}
</ul>
As a workaround,this works well.
I just finished creating a user commenting system on a social networking app I am building with Django (python version 2.7.8, Django verion 1.6).
Everything is working well with the commenting system, but I have encountered an issue. If a user submits a link to an external site in one of their comments, that link appears as plain text. I would like the user submitted link to automatically be viewed as a link to that other users can click on.
Does anyone know a potential solution to this problem?
models.py
class Comment(models.Model):
#Model that defines the Commenting system
created = models.DateTimeField(editable =False)
author = models.CharField(max_length = 200, editable = False)
body = models.TextField()
item = models.ForeignKey(BucketListItem)
def __unicode__(self):
return self.body
comment-template.html
<h2>Comments:</h2>
<br>
{% if comments %}
{% for comment in comments %}
<div class = "comment-div">
<h5>{% avatar comment.author 40 %}</h5>
<h5> {{comment.author}}</h5>
<h5 class ="timesince">{{ comment.created|timesince}} ago.</h3>
<br>
<br>
<p>{{comment.body}}</p>
{% if comment.author == current_user %}
<span class = "fa fa-close"></span>
{% endif %}
</div>
{% endfor %}
<br>
<hr>
<br>
{% else %}
<p>There are no comments yet. Be the first to add one!</p>
{% endif %}
<h5 class = "leave-comment">Leave a Comment Here: </h5>
<br>
<form action="/bucketlist/item/{{id}}/" method = "post" role = "form">
<div class = "form-group">
{% csrf_token %}
{% for field in form %}
{{ field.errors }}
{{ field }}
<br>
{% endfor %}
<br>
<input type = "submit" value = "Submit" class="btn btn-warning">
</div>
<br>
You should be able to do this using the urlize template tag that Django provides.
<p>{{ comment.body | urlize }}</p>
This should convert any links within the body of the comment to an actual <a> tag.