If the declaration does not work in the Django model

advertisements

The if statement within the for loop of the template is not working. It is still showing unavailable items. This is the code of my template

form-template.html

{% for field in form %}
{% if eventprojector.projector.available %}
    <div class="form-group">
    <div class="col-sm-offset-2 col-sm-10">
    <span class="text-danger small">{{ field.errors }} </span>
    </div>
    <label class="control-label col-sm-2">{{ field.label_tag }} </label>
    <div class ="col-sm-10">{{ field }} </div>
    </div>
{%endif %}
{% endfor %}


Here is the refactoring code which will work for both available and unavailable items based on your code:

event/models.py:

from django.db import models

# Create your models here.
class Projectors(models.Model):
    pro = models.CharField(max_length=50)
    available = models.BooleanField(default=True)
    def __str__(self):
        return self.pro

class Eventprojector(models.Model):
    projector = models.ForeignKey(Projectors)
    date = models.DateField(blank=True, null=True)
    start = models.CharField(max_length=50, null=True)
    end = models.CharField(max_length=50, null=True)
    is_accept = models.IntegerField(default=0)

    def get_absolute_url(self):
        return reverse( 'retest:eventprojectorform')
    def __str__(self):
        return str(self.id)

event/urls.py:

from django.conf.urls import url

from . import views

urlpatterns = [
    # ex: /event/1/
    url(r'^(?P<eventprojector_id>[0-9]+)/$', views.projectoraccept, name='projectoraccept'),
]

event/views.py:

from django.shortcuts import render, get_object_or_404
from .models import Eventprojector
# Create your views here.

def projectoraccept(request, eventprojector_id):
    eventprojector = get_object_or_404(Eventprojector, pk=eventprojector_id)
    eventprojector.is_accept = eventprojector.is_accept+1
    eventprojector.save(update_fields=['is_accept'])
    if eventprojector.is_accept == 3:
        eventprojector.projector.available = False
        eventprojector.projector.save(update_fields=['available'])
    return render(request, 'event/projectordetails.html' , {'eventprojector' : eventprojector })

statckoverflow/urls.py:

from django.conf.urls import url, include
from django.contrib import admin

urlpatterns = [
    url(r'^event/', include('event.urls')),
    url(r'^admin/', admin.site.urls),
]

stackoverflow/event/templates/event/projectordetails.html:

{% if eventprojector.projector.available %}
    <div class="form-group">
    <div class="col-sm-offset-2 col-sm-10">
    <span class="text-danger small">TEST</span>
    </div>
    <label class="control-label col-sm-2">TESTTEST</label>
    <div class ="col-sm-10">TESTETST</div>
    </div>
{% endif %}

After all environment is setting, add some test data into tables through admin console:

event_projectors: pro = 'TEST1' and available= True
event_eventprojector :  projector = 'TEST1' date = today, start = 1, end = 2, is_accept = 1

Then run server to start to test:

1, When you first time type in http://127.0.0.1:8000/event/1/, it will show the form like this: (here 'is_accept' field in eventprojector will plus 1 to be 2, check view function)

<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<span class="text-danger small">TEST</span>
</div>
<label class="control-label col-sm-2">TESTTEST</label>
<div class ="col-sm-10">TESTETST</div>
</div>

2, When you enter again the url http://127.0.0.1:8000/event/1/, 'is_accept' field in eventprojector will plus 1 again to be 3, and then it will set eventprojector.projector.available = False, here you cannot see the the element, it is empty in template.

Hope above code can help you to render template projectordetails.html with the parameters eventprojector correctly, note that the render function render(request, 'event/projectordetails.html' , {'eventprojector' : eventprojector }) will only render to tempalte event/projectordetails.html with parameter eventprojector. Probably, there is some mismatch in your template?