Home>

Thanks for your help. Since I am a beginner, I think I made a rudimentary mistake, but the situation will not improve, so please help me.

On a django-based website, a notification field will be provided to display only the titles of news uploaded to mysql on the top page in the form of a list.
Also, at this time, if you click the displayed title, you will be taken to the notification details page.

On the django management site, I can upload the title of the notification to the mysql database without any problem, but I can't display it in the html file.

Corresponding source code

models.py

from django.db import models

class News (models.Model):
    "" "" "Notification column function" "" "" "
    title = models.CharField (verbose_name = "title", max_length = 40, null = True)
    context = models.TextField (verbose_name = "body", blank = True, null = True)
    photo = models.ImageField (verbose_name = "image", blank = True, null = True)
    class Meta:
        verbose_name_plural ='Notification function'
    def __str __ (self):
        return self.title


views.py

from django.views import generic
from .models import News

class IndexView (generic.TemplateView):
    template_name = "hogehoge/index.html"

class RegistrationView (generic.TemplateView):
    template_name = "hogehoge/registration.html"

class LoginView (generic.TemplateView):
    template_name = "hogehoge/login.html"

class LogoutView (generic.TemplateView):
    template_name = "hogehoge/logout.html"

class NewsListView (generic.ListView):
    model = News
    queryset = News.objects.all ()


urls.py

from django.urls import path
from. import views
app_name ='hogehoge'
urlpatterns = [
    path ('', views.IndexView.as_view (), name = "index"),
    path ('registration /', views.RegistrationView.as_view (), name = "registration"),
    path ('', views.NewsListView.as_view (), name = "index"),
]


index.html

{% load static%}
{% block contents%}

     Notice 
    {% for news in news_list%}
    {{news.title}}
    {% endfor%}

{% endblock%}
What I tried

I tried to delete the browser cache, migrate, etc.

Supplementary information (FW/tool version, etc.)

It was developed in PyCharm.
Since I am a beginner, I am very sorry that it will be a rudimentary question. We look forward to hearing from you.

  • Answer # 1

    Premise of answer

    As long as you read the question and the code, we will interpret and answer with the following intentions.

    The process of implementing the top URL on your Django app

    At that time, I want to get the notification information and output it on HTML.

    Answer

    CurrentIndexViewI'm using to respond HTML, but this is justTemplateViewIs inherited fromtemplate_nameNo other changes have been made.
    for that reason,index.htmlWill be used innews_listDoes not exist in the template variable in the first place.

    If you want to prepare a new template variable when using a general view class,get_context_dataYou need to override and add the variables yourself.

    As an example (I have not confirmed the detailed operation):

    class IndexView (generic.TemplateView):
        template_name = "index.html"
        def get_context_data (self, ** kwargs):
            context = super (). get_context_data (** kwargs)
            context ['news_list'] = News.objects.all ()
            return context

    * For details, we recommend reading the Django documentation once.