【问题标题】:Foreign Key not Responding while fetching, Django外键在获取时没有响应,Django
【发布时间】:2026-02-15 17:25:04
【问题描述】:

我正在制作一个 Django 项目,一个企业目录。 在从数据库中获取数据时,我无法获取与外键相关的数据, 请帮忙

我的models.py是::

from django.db import models


class Directory(models.Model):

    Bussiness_name = models.CharField(max_length=300)
    Description = models.CharField(max_length=900)
    Number = models.CharField(max_length=100)
    Web_url = models.CharField(max_length=800)
    Catogory = models.CharField(max_length=200)


    def __unicode__(self):
        return self.Bussiness_name

class Adress(models.Model):
   directory =  models.ForeignKey(Directory)
   adress_name =  models.CharField(max_length=300)
   def __unicode__(self):
        return self.adress_name

class Photos(models.Model):
   directory =  models.ForeignKey(Directory)
   Photo_path =  models.CharField(max_length=100)
   Photo_name =  models.CharField(max_length=100)
   def __unicode__(self):
        return self.Photo_name

我的 view.py 是 ::

# Create your views here.
from django.http import HttpResponse
from crawlerapp.models import Directory
from crawlerapp.models import Adress
from crawlerapp.models import Photos
from django.template import Context, loader
from django.shortcuts import render

def index(request):
    Directory_list = Directory.objects.all()
    t=loader.get_template('C:/Python27/django/crawler/templates/crawlertemplates/index.html')
    c = Context({'Directory_list': Directory_list,})
    return HttpResponse(t.render(c))

def contactus(request):
    Directory_list = Directory.objects.all()
    t=loader.get_template('C:/Python27/django/crawler/templates/crawlertemplates/contactus.html')
    c = Context({'Directory_list': Directory_list,})
    return HttpResponse(t.render(c))

def search(request):
    if 'what' in request.GET and request.GET['what']:
        what = request.GET['what']
        crawlerapp = Directory.objects.filter(Catogory__icontains=what)
        return render(request, 'C:/Python27/django/crawler/templates/crawlertemplates/search.html',
                  {'crawlerapp': crawlerapp, 'query': what})

    elif 'who' in request.GET and request.GET['who']:
        who = request.GET['who']
        crawlerapp = Directory.objects.filter(Bussiness_name__icontains=who)
        return render(request, 'C:/Python27/django/crawler/templates/crawlertemplates/search.html',
                  {'crawlerapp': crawlerapp, 'query': who})

    else:
        message = 'You submitted an empty form.'
    return HttpResponse(message)

当我试图从我的数据库(MySQL)中获取数据时,它只是获取类目录的数据,形成 Models.py

而我在html页面中用于获取的代码是::

<p>You searched for: <strong>{{ query }}</strong></p>

{% if crawlerapp %}

    <p>Found {{ crawlerapp|length }} in this Category{{ crawlerapp|pluralize }}.</p>
    <ul>
        {% for Directory in crawlerapp %}
        <li>Business Name:  {{ Directory.Bussiness_name }}</li>
        Description:        {{ Directory.Description }}</br>
        Contact Number: {{ Directory.Number }}</br>
        Web_URL:          {{ Directory.Web_url }}</br>
        Adress:               {{ Adress.adress_name }}</br>
        Photo:                 {{ Photos.Photo_name }}</br></br>
        {% endfor %}
    </ul>
{% else %}
    <p>No Business matched your search criteria.</p>
{% endif %}

我得到的输出有点像下面

例如:您搜索:电脑维修

               Found 1 in this Categorys

               Business Name: C S Tecj
               Description: hello
               Contact Number: 098754
               Web_URL: www.rrrrrr.co
               Adress: 
               Photo: 

请帮我获取外键的数据,也就是 Adress: 和 Photo:

请帮忙解决这个问题。

【问题讨论】:

  • 您将遇到使用绝对路径进行模板渲染的问题。

标签: python django django-models django-templates django-views


【解决方案1】:

您可以按相反的顺序访问ForeignKey 对象,如下所示:

{% for Directory in crawlerapp %}
<li>Business Name:  {{ Directory.Bussiness_name }}</li>
    Description:    {{ Directory.Description }}</br>
    Contact Number: {{ Directory.Number }}</br>
    Web_URL:        {{ Directory.Web_url }}</br>
    Adress:         {% for Adress in Directory.adress_set.all %}{{ Adress.adress_name }}</br>{% endfor %}
    Photo:          {% for Photos in Directory.photos_set.all %}{{ Photos.Photo_name }}</br>{% endfor %}</br>
{% endfor %}

【讨论】:

  • 定义项目根目录,去掉模板的绝对链接
  • 请解决那个路径问题ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo请解决............
  • 创建一个新问题。应该有人来帮助你。