Django templates folders

后端 未结 4 468
遥遥无期
遥遥无期 2020-12-02 13:39

I\'m experimenting with Django, and figuring out how to set urls.py, and how the URLs work. I\'ve configured urls.py in the root of the pro

相关标签:
4条回答
  • 2020-12-02 13:55

    I would re-organize the urls as such:

    urlpatterns = patterns('',
        (r'^admin/doc/', include('django.contrib.admindocs.urls')),
        (r'^admin/', include(admin.site.urls)),
        (r'^tinymce/', include('tinymce.urls')),
        (r'^blog/', include('hellodjango.blog.urls')),
        (r'^$', direct_to_template, {"template": "base.html"}),
    )
    

    Patterns are matched by their specificity, so I tend to put the more specific patterns first. Otherwise you might see some unexpected behavior. Give that a try, and if it's still loading a template from your blog on a request to /, we'll dig deeper.

    0 讨论(0)
  • 2020-12-02 13:56
    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('',
        url(r'^blog/', include('hellodjango.blog.urls')),
        url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
        url(r'^admin/', include(admin.site.urls)),
        url(r'^tinymce/', include('tinymce.urls')),
    )
    
    urlpatterns += patterns(
        'django.views.generic.simple',
        (r'^', 'direct_to_template', {"template": "base.html"}),
    )
    
    0 讨论(0)
  • 2020-12-02 14:09

    Did you set TEMPLATE_DIRS in your settings.py? Check and make sure it is set up correctly with absolute paths. This is how I make sure it is properly set:

    settings.py

    PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))
    
    TEMPLATE_DIRS = (
        # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
        # Always use forward slashes, even on Windows.
        # Don't forget to use absolute paths, not relative paths.
        os.path.join(PROJECT_ROOT, 'templates').replace('\\','/'),
    )
    
    # List of callables that know how to import templates from various sources.
    TEMPLATE_LOADERS = (
        'django.template.loaders.filesystem.Loader',
        'django.template.loaders.app_directories.Loader',
    #     'django.template.loaders.eggs.Loader',
    )
    

    This way, I have a templates folder in my project root that is used for non-app templates and each app has a templates/appname folder inside the app itself.

    If you want to use a template from the root template folder, you just give the name of the template like 'base.html' and if you want to use an app template, you use 'appname/base.html'

    Folder structure:

    project/
      appname/
        templates/ 
          appname/  <-- another folder with app name so 'appname/base.html' is from here
            base.html
        views.py
        ...
    
      templates/    <-- root template folder so 'base.html' is from here
        base.html
    
      settings.py
      views.py
      ...
    
    0 讨论(0)
  • 2020-12-02 14:17

    I think it depends what you want your home page to be. If its simply a page with links off to other parts of your site then catherine's answer is a nice clean way.

    If you want the root of your site to be your blog for example I would do this:

    urlpatterns = patterns('',
        # Django Admin
        url(r'^admin/', include(admin.site.urls)),
        # Tiny MCE Urls
        url(r'^tinymce/', include('tinymce.urls')),
        # Other App
        url(r'^other/', include('projectname.other.urls', namespace='other')),
        # Blog App
        url(r'^', include('projectname.blog.urls', namespace='blog')),
    )
    

    Also don't forget to name space your url includes: https://docs.djangoproject.com/en/dev/topics/http/urls/#url-namespaces

    0 讨论(0)
提交回复
热议问题