Django template

62 views
Skip to first unread message

Tom Zhang

unread,
Feb 8, 2019, 2:07:00 PM2/8/19
to Django users
Hi, all,

If I don't want to use Django template engine and I just want to use regular html/javacript, how do I configure settings.py?
I am thinking about the portability issue. For example, if I want to move away from django in the future, I hope my templates can still be used in other frameworks, at least, with minimum changes.
My settings are:

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')],
        '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',
            ],
        },
    },
]

Thanks,

Tom

Mikko Meronen

unread,
Feb 8, 2019, 2:30:39 PM2/8/19
to django...@googlegroups.com
Hi,

Have to say that Im still beginner in Django and coding, but I think you just create you html files to your templates folder, and in the end you are basically using just html. Django can just find your html file from templates folder.

Im just learning javascript and I assume i can save my js-file to templates folder as well and connect it to my html.

-Mikko

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/237d37a9-a913-430c-8861-eeedb8f6bece%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Tom Zhang

unread,
Feb 8, 2019, 2:43:11 PM2/8/19
to Django users
I will give it a try first. Thanks

Nitin Kalmaste

unread,
Feb 8, 2019, 8:35:28 PM2/8/19
to django...@googlegroups.com
You are correct,
You are using HTML and JavaScript as template in django. You need to store static files like css/images/JavaScript in static folder and configure static folder in the settings file

Tom Zhang

unread,
Feb 9, 2019, 1:14:16 AM2/9/19
to Django users
Thanks. But if I do this way, how do I transfer data between back-end and front-end? Do I use post request?

Mikko Meronen

unread,
Feb 9, 2019, 1:42:19 AM2/9/19
to django...@googlegroups.com
Hi,

You can connect your backend to frontend using views.py file. In views.py you can get and sort your data from SQL and send it to your html. 

You probably should do tutorial or watch youtube videos (as I did since I get bored in reading very fast) to learn more. CodingEntrepreneur has done quite up to date videos, though you might need to check some point from tutorial sheets as well.
Here is a link to backend-frontend video, but better to see the series from the beginning when you are new:


My example from views.py, where I get and sort data from my database and connect it to my webpage. In your html you need some code to get the data from views.py.

views.py

from django.shortcuts import render
from .models import news 

def Home(request):
    worlddata = news.objects.filter(subsection__exact='World').order_by('-created')[:8]
    args = {'worlddata': worlddata}
    return render(request, "news/home.html", args)

In home.html (bolded ones is the code you need to connect to your views.py)

{% for news in worlddata %}
<h4> <a href="{{ news.url }}" target="_blank">{{ news.title }}</a> </h4>
{% endfor %}

-Mikko


Tom Zhang

unread,
Feb 9, 2019, 1:47:46 AM2/9/19
to Django users
Thanks. I know how to do this according to Django way. But if you use Django's way to transfer data, it makes it difficult to move to other frameworks, right? What do you think?

Mikko Meronen

unread,
Feb 9, 2019, 1:58:42 AM2/9/19
to django...@googlegroups.com
Hi,

I haven't used any other framework, so maybe someone else can better answer that. But using my logic I think that if I want to change framework, I just have to know how to connect again my database to my html, which is just to learn the new framework logic I guess. But as I said, hopefully someone wiser can answer to this :p

-Mikko

PASCUAL Eric

unread,
Feb 9, 2019, 6:43:33 AM2/9/19
to django...@googlegroups.com
Hi,

Templates are nothing more than HTML files with special tags inside. In their simplest form, they can be plain HTML without any templating tags and they will thus be rendered without any change.

In conclusion, to keep it simple and still let you the opportunity to benefit from templating features if ever the need arises in the future, let settings as they are and follow template view examples. The overhead will be minimal for such "static" templates since the engine provides a quite efficient preprocessing and caching mechanism and in any case, negligible compared to DB or network related stuff.

Anyway, if for any reason you do not want to keep the templating engine in the path at all, your views can return basic HTTP responses containing static HTML data, either stored as constant strings in your code (not the cleanest option) or, better, as resources in your project (e.g. plain files somewhere they can be read from the app code).

Best 

Eric


From: django...@googlegroups.com <django...@googlegroups.com> on behalf of Tom Zhang <compute...@gmail.com>
Sent: Friday, February 8, 2019 19:59
To: Django users
Subject: Django template
 
Hi,

If I don't want to use any Django template and I just want to use regular html/javascript, how should I setup the template config in settings.py?

Right now, my settings are:

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')],
        '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',
            ],
        },
    },
]

Thanks,

Tom

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

rich gang

unread,
Feb 9, 2019, 8:32:19 AM2/9/19
to django...@googlegroups.com
Hi guys, 
I just finished learning python on sololeran.com.
And I wanna start django, so I was wondering If anybody would wanna help me personally.
Like I said I have a fair knowledge about python.
I would be grateful.
thanks,

Reply all
Reply to author
Forward
0 new messages