[Django]-How to redirect via TemplateView.as_view to .html file

6๐Ÿ‘

โœ…

I managed to find my error:

this is how my urls.py looked:

from django.conf.urls import patterns, include, url
from django.views.generic import TemplateView
from django.contrib import admin

admin.autodiscover()

urlpatterns = patterns('',
    url(r'^admin/', include(admin.site.urls)),
    url(r'^$', 'joins.views.home', name='home'),
    url(r'^(?P<ref_id>.*)$', 'joins.views.share', name='share'),
    url(r'^impressum/$', TemplateView.as_view(template_name='impressum.html')),
)

I put the impressum url on the top and then it worked out. I remembered from a tutorial that there is a certain logic how django checks the urls line by line and if you have an url with an extension you have to put before the url without any in order to make it work

๐Ÿ‘คDaniel

2๐Ÿ‘

This might help now.

Template.as_view()

This function looks the default django library folder.
So, add your template directory to your django setting.

I guess you have setting in setting_old.py ( by default setting.py).
Because the function looks TEMPLATES directory first,
add your template directory, after the manage.py folder, to the setting like below.


TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            'templates',   # <- here 
        ],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]
๐Ÿ‘คPeter

Leave a comment