-1

我正在制作一个 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: 

请帮我获取外键的数据,也就是地址:和照片:

请帮助解决这个问题。

4

1 回答 1

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 %}
于 2013-07-15T16:54:51.570 回答