Django在获取时没有响应外键

时间:2013-07-15 16:04:26

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

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

我的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 %}

我得到的OutPut有点像下面

例如:您搜索过:计算机修复

               Found 1 in this Categorys

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

请帮我拿取外键的数据,即Adress:和Photo:,

请帮助解决这个问题。

1 个答案:

答案 0 :(得分: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 %}