-1

我正在制作一个 Django 项目,一个企业目录。在这里,我使用 ABSOULATE PATHS 进行模板渲染,我认为这可能会在将来产生问题。请查看我的代码并建议我如何解决此问题,以免将来产生任何问题。

请帮忙

我的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)

我的 urls.py 是::

from django.conf.urls import patterns, include, url
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'crawler.views.home', name='home'),
    # url(r'^crawler/', include('crawler.foo.urls')),

    # Uncomment the admin/doc line below to enable admin documentation:
    # url(r'^admin/doc/', include('django.contrib.admindocs.urls')),

    # Uncomment the next line to enable the admin:
    url(r'^admin/', include(admin.site.urls)),
    url(r'^crawlerapp/$', 'crawlerapp.views.index'),
    url(r'^crawlerapp/contactus/$', 'crawlerapp.views.contactus'),
    url(r'^crawlerapp/search/$', 'crawlerapp.views.search'),
)

我有 3 个 html 页面索引、联系方式和搜索。请建议绝对路径的替代方案,以便如果我其他人从 GITHUB 克隆它并尝试运行它,它不会产生错误。

请帮助解决这个问题。

4

4 回答 4

2

在您的项目 settings.py 文件中:

在顶部添加这个

import os.path
PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))

然后设置模板路径这样做:

TEMPLATE_DIRS = (
    os.path.join(PROJECT_ROOT, "templates"),
)
于 2013-07-15T18:58:09.363 回答
2

你考虑过阅读 Django 教程吗?. 不要忘记在您的 settings.py 文件中设置TEMPLATE_DIRS,可以在另一个答案中找到这样做的好技巧;Django模板路径

于 2013-07-15T17:54:00.150 回答
2

您应该在列表中的 setting.py 文件中列出您是模板目录TEMPLATE_DIRS

无论您是否这样做,您都应该使用os.path模块的功能动态生成绝对路径。

os.path.abspath(__file__)

将返回调用它的文件的绝对路径。

os.path.dirname('some/path')

将返回最后一个目录的路径'some/Path'

通过组合它们,您可以获得在不同系统上保持准确的绝对路径,例如

os.path.abspath(os.path.dirname(os.path.dirname(__file__)))

将返回到包含当前文件的目录上一级目录的绝对路径。

去阅读模块的文档os.path您可能也需要使用os.path.join

于 2013-07-15T17:58:51.180 回答
0

有了这个,您将来有义务重新检查所有文件以更改路径,以防您更改目录:在您的settings.py文件中定义:

import os.path

TEMPLATE_DIRS = (
 os.path.join(os.path.dirname(__file__),'templates').replace('\\','/'),
 '/path/to/my/project/'
 '/path/to/my/project/template/', # for the apps
)

templates/您的模板的目录在哪里。

于 2013-07-15T17:58:30.100 回答