1π
β
To fix your specific issue, you can do something like this:
class Environment(models.Model):
name = models.CharField(max_length=128, unique=True)
app_list = models.CharField(max_length=128, blank=True)
def __str__(self):
return self.name
def format_apps_list(self):
app_list = self.app_list or "" #if app_list is blank
return (" ".join(["[%s] " % a for a in app_list.split(',')])
and you can call the format
helper method in the template β feel free to modify it to your usecase.
{% for environment in environments %}
<li><a href="/dashboard/environment/{{ environment.url }}">{{ environment.name }}</a> {{environment.format_apps_list}}</li>
I see a few things that could be changed in this app.
I would also recommend using slugs
β example this app is great (django-autoslug-field). One approach for this would be:
from django.db import models
class Environment(models.Model):
name = models.CharField(max_length=128, unique=True)
slug = AutoSlugField()
def __str__(self):
return self.name
class App(models.Model):
environment = models.ForeignKey(Environment)
name = models.CharField(max_length=128)
slug = AutoSlugField()
This would give you the flexibility to analyze which apps are in an environment, and at the same time, what environments does an app belong to.
Your template would be
{% for environment in environments %}
{% for app in environment.app_set.all %}
{{app.name}}
{% endfor %}
{% endfor %}
Also, now you can process the url by slug, instead of the name β which would eliminate all the .replace(..)
hacks
π€karthikr
Source:stackexchange.com