Mapping views.py in Django (Python) -
i need mapping views in django. app pretty simple -- it's status page listing of our environments , corresponding apps. far have env's listed so:
env_1 env_2 env_3
but want is: (where [app*] app_list in environment class -- see below)
env_1 [app1] [app2] [app3] ... env_2 [app1] [app2] [app3] ... env_3 [app1] [app2] [app3] ...
here's models.py (app_list = app1, app2, etc.)
- - update - -
from django.db import models 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(self): app_list = self.app_list or "" return (" ".join(["[%s] " % in app_list.split(',')]))
and views.py:
from django.shortcuts import render dashboard.models import environment, page def index(request): environment_list = environment.objects.order_by('name') context_dict = {'environments': environment_list} environment in environment_list: environment.url = environment.name.replace(' ', '_') return render(request, 'dashboard/index.html', context_dict) def environment(request, environment_name_url): environment_name = environment_name_url.replace('_', ' ') context_dict = {'environment_name': environment_name} try: environment = environment.objects.get(name=environment_name) context_dict['environment'] = environment except environment.doesnotexist: pass return render(request, 'dashboard/environment.html', context_dict)
and index.html
<body> <h1>title</h1> {% if environments %} td> {% environment in environments %} <li><a href="/dashboard/environment/{{ environment.url }}">{{ environment.name }}</a>{{environment.format_apps_list}}</li> </td> {% else %} <strong>there no environments present.</strong> {% endif %} </body>
to fix specific issue, can 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 blank return (" ".join(["[%s] " % in app_list.split(',')])
and can call format
helper method in template - feel free modify usecase.
{% environment in environments %} <li><a href="/dashboard/environment/{{ environment.url }}">{{ environment.name }}</a> {{environment.format_apps_list}}</li>
i see few things changed in app. recommend using slugs
- example app great (django-autoslug-field). 1 approach 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 give flexibility analyze apps in environment, , @ same time, environments app belong to.
your template be
{% environment in environments %} {% app in environment.app_set.all %} {{app.name}} {% endfor %} {% endfor %}
also, can process url slug, instead of name - eliminate .replace(..)
hacks
Comments
Post a Comment