1

models.py

class MyVideo(models.Model):
    title = models.CharField(max_length=100, null=True, default='')
    seotitle = models.CharField(max_length=100, null=True, default='')
    keywords = models.CharField(max_length=150, null=True, default='')
    status = models.IntegerField(default=1)

serializers.py

class MyVideoSerializer(serializers.ModelSerializer):
    class Meta:
        model = MyVideo
        fields = '__all__'

views.py

class My(viewsets.ModelViewSet):
    queryset = MyVideo.objects.all()
    serializer_class = MyVideoSerializer

    renderer_classes = (JSONRenderer, TemplateHTMLRenderer,)
    template_name = "my.html"

    def get(self, request, *args, **kwargs):
        # ??????

    def get_query(self):
        # ??????

urls.py

urlpatterns = [
    path('my/', views.My), # ???????? anything wrong here?
]

my.html

<html><body>
    <h1>My video</h1>
    <ul>
    {% for d in data %}
        <li>{{ d.title }}</li>  # ?????? anything wrong here?
        <li>{{ d.seotitle }}</li>
        <li>{{ d.keywords }}</li>
    {% endfor %}
    </ul>
</body></html>

I have a MyVideo model which store several videos record in the database. What I wanna implement is that to display the information of those videos through the my.html.

e.g. http://127.0.0.1:8000/my/103 can access the video which id=103, and on this page display its fields (title, seotitle, keywords, etc.).

Any nice implementation or suggestion? Thanks!

**UPDATE

<html><body>
<h1>My video</h1>
<ul>
    {{ data }}
</ul>
</body></html>
9
  • Where you have inserted # ???? What do you have there? Commented Aug 1, 2018 at 14:56
  • @Lewis Nothing, I don't know how to do next... Commented Aug 1, 2018 at 14:58
  • Why do you think you need to define those methods at all? Commented Aug 1, 2018 at 14:59
  • @DanielRoseman I'm not sure do I need override those method or not, I just followed the API docs and tried. Commented Aug 1, 2018 at 15:03
  • It's the viewsets docs you should be reading. You don't need those methods, but you do need a router in the urls. Commented Aug 1, 2018 at 15:06

1 Answer 1

1
   from django.shortcuts import render
    class My(viewsets.ModelViewSet):
        queryset = MyVideo.objects.all()
        serializeddata = MyVideoSerializer(queryset,many=true)

    renderer_classes = (JSONRenderer, TemplateHTMLRenderer,)
    template_name = "my.html"

    def get(self, request, *args, **kwargs):
        return render_to_response(template_name, {'data': serializeddata.data})
    #or use below
      #return render(
    #request, 
    #template_name=template_name,
    #{'data': serializeddata}
#)

    def get_query(self):
        # ??????

UPDATE

models.py

"""
Definition of models.
"""

from django.db import models

# Create your models here.
class MyVideo(models.Model):
    title = models.CharField(max_length=100, null=True, default='')
    seotitle = models.CharField(max_length=100, null=True, default='')
    keywords = models.CharField(max_length=150, null=True, default='')
    status = models.IntegerField(default=1)

serializers.py

from rest_framework import serializers
from app.models import MyVideo

class MyVideoSerializer(serializers.ModelSerializer):
    class Meta:
        model = MyVideo
        fields = '__all__'

my.html

<html><body>
    <h1>My video</h1>
    <ul>
    {% for d in data %}
        <li>Title: {{ d.title }}</li>  
        <li>SEO Title:{{ d.seotitle }}</li>
        <li>KeyWords {{ d.keywords }}</li>
    {% endfor %}
    </ul>
</body></html>

views.py

def my(request):
    """Renders the contact page."""
    assert isinstance(request, HttpRequest)
    queryset = MyVideo.objects.all()
    serializer_class = MyVideoSerializer(queryset,many=True)
    #datan = {"title":"Test Title"}
    return render(
        request,
        'app/my.html',
        {
            'data':serializer_class.data,
        }
    )

NOTE: You can add if(request.method == GET): elif(request.method == POST)

OUTPUT:

enter image description here

Sign up to request clarification or add additional context in comments.

16 Comments

Let me Know if Any Issue Arrives!
"'My' should either include a serializer_class attribute, or override the get_serializer_class() method." You changed the original serializer_class which brings this error. Any ideas?
I added "serializer_class = MyVideoSerializer" back and it runs. However, the "{'data': serializeddata} didn't rendered to the HTML page, but it returns the data if I just print(serializeddata) in the code.
My mistake, make it serializeddata.data and let me know the result!
serializeddata = MyVideoSerializer(queryset, many=true) also do it like this as the result may be many!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.