-1

I am making a Django Project, A Business Directory. Here I have used ABSOULATE PATHS for Template rendering, I thnk this might create problem in future. Please look into my codes and Suggest me how to solve this problem so that it does not create any 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 is ::

# 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)

And my urls.py is::

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'),
)

I have 3 html pages INDEX, CONTACTUS, and SEARCH. Please suggest an alternative of Absolute path so that it create no errors if I someone else clones it from GITHUB and try to run it.

Please help to solve this.

Abhimanyu
  • 81
  • 6

4 Answers4

2

Have you considered reading the Django tutorial?. Don't forget to set TEMPLATE_DIRS in your settings.py file, a good tip for doing so can be found on another answer; Django template Path

Community
  • 1
  • 1
Sam Nicholls
  • 861
  • 4
  • 16
2

You should list you're template directories in your setting.py files, in the TEMPLATE_DIRS list.

Whether you do that or not, you should dynamically generate the absolute path by using the os.path module's function.

os.path.abspath(__file__)

will return the absolute path to the file it's called in.

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

will return the path to the last directory in 'some/Path'

By combining them you can get absolute pathes which will remain accurate on different systems, eg

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

will return an absolute path to the directory one level above the one containing the current file.

Go read the docs for the os.path module. You'll probably need to use os.path.join as well.

astrognocci
  • 1,057
  • 7
  • 16
2

In your project settings.py file:

Add this at the top

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

Then to set template path do this:

TEMPLATE_DIRS = (
    os.path.join(PROJECT_ROOT, "templates"),
)
Shanki
  • 167
  • 3
0

With this you going to be in the obligation in a future to recheck your all files to change the path in case if you change directories so: in you settings.py file define:

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
)

Where templates/ is the directory for your templates.

Sam Nicholls
  • 861
  • 4
  • 16
drabo2005
  • 1,076
  • 1
  • 10
  • 16