TransWikia.com

Problema al visualizar pagina con django

Stack Overflow en español Asked on December 7, 2021

Quiero hacer una pagina llamada Topics y me tira el siguiente error:

path("topics/", views.topics, name='topics'), 
AttributeError: module 'learning_logs.views' has no attribute 'topics'

Les dejo los archivos a continuacion:

MODELS.PY

from django.db import models

class Topic(models.Model):
   #A topic the user is learning about
   text = models.CharField(max_length=200)
   data_added = models.DateTimeField(auto_now_add=True)

def __str__(self):
    """Return a string representation of the model."""
    return self.text

VIEWS.PY

from django.shortcuts import render
from .models import Topic

def index(request):
    """The home page for Learning Log"""
    return render(request, 'learning_logs/index.html')

def views(request):
    #Show all topics
    topics = Topic.objects.order_by('date_added')
    context = {'topics':topics}
    return render(request, 'learning_logs/topics.html', context)

URLS.PY

"""Defines URL patterns for learning_logs."""
from django.urls import path
from . import views

app_name = 'learning_logs'

urlpatterns = [
    # Home Page
    path("", views.index, name='index'),
    # Show all topics.
    path("topics/", views.topics, name='topics'),
]

2 Answers

En tus urls defines que el path "/topics/" va a la vista views.topics lo que esta bien, el problema es que en tus views.py no tienes la funcion .topics y ese es tu error se corrige asi

from django.shortcuts import render
from .models import Topic

def index(request):
    """The home page for Learning Log"""
    return render(request, 'learning_logs/index.html')

def topics(request):
    #Show all topics
    topics = Topic.objects.order_by('date_added')
    context = {'topics':topics}
    return render(request, 'learning_logs/topics.html', context)

Answered by Iankos on December 7, 2021

Hola no sera que por ejemplo en Url.py- tu en index.html estas llamando a una funcion "index", pero en el caso de path("topics/", views.topics, name='topics'), tu aqui llamas a una variable "topics" no a una funcion. seria esto---path("topics/", views.views, name='topics').

Igual me equivoco no soy my entendido, o sino prueba a cambiar la ruta topics.html

Answered by samuandis on December 7, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP