Feb-13-2019, 05:46 PM
Hi,
I'm trying to create a list of url links in categories on a site at work, they use about 80 web based tools for content discovery and other things. I have built the site using django and currently the links are all hard coded into the side bar. This would usually be fine, but the links change on a daily basis as there are so many and tools migrate to different servers etc, so I wanted to serve the links dynamically, but I struggling to find out how this is done. In the python I have learned, I would just do:
Any pointers would be really helpful as I have been through the django tutorial and I have nearly finished a full on course on Django that has taken me a good 3 months to go through and unfortunately, that does not cover this issue either. I'm assuming there is a way to work with multiple models, as I thought that was the the point to have two or more tables and take data from both, but django does not make this easy for newbies.
This is what I have so far:
model.py
I'm trying to create a list of url links in categories on a site at work, they use about 80 web based tools for content discovery and other things. I have built the site using django and currently the links are all hard coded into the side bar. This would usually be fine, but the links change on a daily basis as there are so many and tools migrate to different servers etc, so I wanted to serve the links dynamically, but I struggling to find out how this is done. In the python I have learned, I would just do:
for i in categories: print(i) for j in links: print(j)But I can't seem to do that in django. So far I am only able to use one model per view and one view per template, so I haven't a clue as to how you would do this. Ultimately I will be using the nested loops in the template so that I can get the javascript that opens and closes the nested links correctly, but can't figure out how to take a category from the category model and the links from the links model. I tried using the foreign key only to have the category name printed as many times as there is links in the category. There are basically 10 categories and anything from 3 links to 20+ links in each category.
Any pointers would be really helpful as I have been through the django tutorial and I have nearly finished a full on course on Django that has taken me a good 3 months to go through and unfortunately, that does not cover this issue either. I'm assuming there is a way to work with multiple models, as I thought that was the the point to have two or more tables and take data from both, but django does not make this easy for newbies.
This is what I have so far:
model.py
from datetime import datetime from django.db import models class Category(models.Model): ''' Category of links in the Sidebar ''' name = models.CharField(max_length=30,unique=True) def __str__(self): return self.name class Link(models.Model): category = models.ForeignKey(Category, on_delete=models.CASCADE) date = models.DateTimeField(auto_now_add=True) name = models.CharField(max_length=24,unique=True) url = models.URLField(unique=True) def __str__(self): return self.nameview.py
from django.shortcuts import render, redirect from django.template import RequestContext from django.views.generic import View,TemplateView,ListView,DetailView from . import models class TestpageView(ListView): context_object_name = 'link_tree' model = models.LinkI can work with the other areas of django, settings urls etc. but trying to work with the logic in django is really tricky. Any pointers would be really appreciated.