Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django pass variable into template

Hi thank you for helping, I'm poor in coding.

To point: I'm doing a Django project that pass data form data-base to front-end; but right now i can't even pass anything views of Django into templates, I suspect i'm passing the wrong variable types; please do comment on your thought.

This is my code on views.py:

from django.shortcuts import render

def index (requset):
    return render(requset,'myapp/index.html') # link to be able open frountend

def testdex(requset):
    text = "hello world"
    context ={'mytext' : text }
    return render(requset,'myapp/inculdes.html', context)

so my variable will be pass into inculdes where extend to index page

This my codes on in inculdes.html:

{% exntends "myapp/index.html" %}

{% block includes %}
{{ mytext }}
{% endblock includes %}

this my code on index.html:

<body>
{% block includes %} {% endblock includes %}    
</body>

Thanks again on giving me your time to help me and appreciate it if could write me some code because try fix this for whole week

like image 418
Rookies DJ Avatar asked Oct 30 '18 04:10

Rookies DJ


People also ask

What are variables in Django templates?

Django templates allow the passing of data from view to template and also provide some limited features of programming such as variables, for loops, etc. Variables in Django are used for passing the data from view to template. In this article, we will learn about variables in Django Templates.

How do you pass a variable from Python to a template?

How do you pass a Python variable to a template? And this is rather simple, because Django has built-in template modules that makes a transfer easy. Basically you just take the variable from views.py and enclose it within curly braces { { }} in the template file.

How do I get data from the members model in Django?

To get data from the Members model, we will have to import it in the views file, and extract data from it in the view: We use the Django template tag {% for %} to loop through the members. You will learn more about template tags in the next chapter.

What does a dot mean in a variable in Django?

When the Django template engine encounters a variable, it replaces that variable with the same variable passed in the template engine. An underscore or a dot (but they must not start with a dot or underscore). A dot in a variable name has a special meaning during template rendering.


1 Answers

I found out why Django can't pass variables to HTML because;

I didn't have my apps url activated the function/model in views

I feel so embarrasses, for such simple mistakes.

All I need to do is add this code in my apps url

urlpatterns = [
path('', views.timedex, name='timedex'), #need add this 
path('', views.index, name='index'),
]
like image 138
Rookies DJ Avatar answered Sep 20 '22 22:09

Rookies DJ