Error in html Django?

Asked

Viewed 334 times

2

Good morning guys, I’m having a little problem referencing my variable in the html of Django. I’m a beginner in the web part, I’m trying to create a site for a project, so during the html construction, I tried to perform a "for" reference to my view.py, but it does not find the text saved in the "Texts" variable".

Additional information

1) I’m working with Sublime Text 3.(If it makes a difference)

2) I have already added the plugins: Django, anaconda(configured), Css3, Html5 and ement.

Could help me understand this problem?

Follows html code:

   </div>
    <div class="container">
		<div class="jumbotron page-header">
		  <h1>{{ title|capfirst }}</h1>
		  <p> 
		  	  {% for item in texts %}
		  	  {{item}},
		  	  {% endfor %}
		  	   
		  </p>

View.py from Django:

# coding=utf-8

from django.shortcuts import render
from django.http import HttpResponse


def index(request):
     texts = ['Lorem ipsum dolor sit amet, consectetur adipisicing 
     elit, sed do eiusmod tempor incididunt ut labore et dolore magna 
     aliqua']
context = {
    'title':'django e-commerce',
    'texts':texts
}
     return render(request, 'index.html', context)

1 answer

1


I don’t know if it was a mistake in control-c/control-v, but the way your code should not even compile, your Texts is breaking line without the scape for it. Besides, it makes no sense to put only one string in the txts list to then access through one for. Try following the steps below and see if it works:

py views.:

from django.shortcuts import render
def index(request):
    texts = ['Primeira String', 'Segunda string', 'Terceira String']
    context = { 'title':'django e-commerce', 'texts':texts }

    return render(request, 'core/index.html', context)

index.html

{% for t in texts %}
    <p>{{ t }}</p>
{% endfor %}

When you run it should appear in Browse:

inserir a descrição da imagem aqui

  • Grateful Sidon... I’m actually typing even though thanks for the comment I’ll check and fix. Grateful fellow.

  • If you do what I "said" you will get the desired result, without a shadow of a doubt

  • If the answer has been resolved, consider giving the acceptance in the question.

  • Erdão Sidon, yes the answer was very satisfactory... But as I said, I’m starting my life in Django... Soon she was creating a variable to test the use of templates in the framework. But she made it easy... Nothing like experienced colleagues. Big hug.

  • Then consider accepting the answer by clicking on the sign next to the number of votes. Thank you,

  • You saw in my example, I also create a variable, the same way you did.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.