Why Django code doesn't reflect in website

Viewed 40

i made a simple code in django but the Charfield and URlField doesn't show which was given by admin.

#index.html

{%for des in des %}
<a href="{{des.link.url}}">Click here</a>
<p>{{des.txt}}</p>
{%endfor%}

#view.py

from django.shortcuts import render
from .models import destination
def index(request):
    des = destination.objects.all()

    return render(request, 'index.html', {'des': des})

#models.py

from django.db import models

# Create your models here.
class destination(models.Model):
   link = models.CharField(max_length=200)
   txt = models.CharField(max_length=100, default=False)

The image of admin is show.. link and text is provided by admin

enter image description here

Image shows the html code which doesn't appear in website.enter image description here

2 Answers

It seems the issue you are facing is that you are using des for the variable in the for loop and also for the collection you are iterating, can you change it to:

{%for de in des %}
  <a href="{{de.link}}">Click here</a>
  <p>{{de.txt}}</p>
{%endfor%}

Also, consider renaming your class destination to Destination.

The problem is that you're invoking an nonexistent attribute (url). Just replace with the following:

{%for d in des %}
    <a href="{{ d.link }}">Click here</a>
    <p>{{ d.txt }}</p>
{% endfor %}

Also, please note that you should not use the same name for the iterable and the variable inside the loop, as was well said by lmiguelvargasf

Related