python - Alternative for Absolute Path in Django -


i making django project, business directory. here have used absoulate paths template rendering, thnk might create problem in future. please codes , suggest me how solve problem not create problem in future.

please help

my models.py is::

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 

my view.py ::

# create views here. django.http import httpresponse crawlerapp.models import directory crawlerapp.models import adress crawlerapp.models import photos django.template import context, loader 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 , request.get['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 , request.get['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 empty form.'     return httpresponse(message) 

and urls.py is::

from django.conf.urls import patterns, include, url # uncomment next 2 lines enable admin: django.contrib import admin admin.autodiscover()  urlpatterns = patterns('',     # examples:     # url(r'^$', 'crawler.views.home', name='home'),     # url(r'^crawler/', include('crawler.foo.urls')),      # uncomment admin/doc line below enable admin documentation:     # url(r'^admin/doc/', include('django.contrib.admindocs.urls')),      # uncomment next line enable 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'), ) 

i have 3 html pages index, contactus, , search. please suggest alternative of absolute path create no errors if else clones github , try run it.

please solve this.

you should list you're template directories in setting.py files, in template_dirs list.

whether or not, should dynamically generate absolute path using os.path module's function.

os.path.abspath(__file__) 

will return absolute path file it's called in.

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

will return path last directory in 'some/path'

by combining them can absolute pathes remain accurate on different systems, eg

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

will return absolute path directory 1 level above 1 containing current file.

go read docs os.path module. you'll need use os.path.join well.


Comments