简体   繁体   中英

How to get the content from my database in views.py? [Django]

I am trying to print the content fields from my database, Here's my models.py file:

class Post(models.Model):
    title = models.CharField(max_length=100)
    content = models.TextField()
    read_time = models.TimeField(null=True, blank=True)
    view_count = models.IntegerField(default=0)

Here's my views.py file:-

class PostDetailView(DetailView):
    model = Post
    def get_object(self):
        obj = super().get_object()
        obj.view_count += 1
        obj.save()
        return obj
    
    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        all_texts = {
            'texts': context.content
        }
        print(all_texts[texts])
        return context

I am trying to access all the data's from the content field from my database, But the above way is not working, Is there any way I can access all the data's from the content field, because I have to perform some action on these fields, like calculate the read_time of any content, based on the length of it.

Just query all objects and loop the queryset to manipulate them according to your needs like so:

def your_view(self, **kwargs):

    # Get all table entries of Model Post
    queryset = Post.objects.all()

    # Loop each object in the queryset
    for object in queryset:

    # Do some logic
        print(object.content)

    [...]
    return (..., ...)

You do not have to override the .get_queryset(…) method [Django-doc] for that, since the object is already passed to the context. You can simply render it in the template with:

{{ object }}

In case you really need this in the context, you can implement this as:

class PostDetailView(DetailView):
    model = Post
    
    # …
    
    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context.update(
            texts=self
        )
        return context

In case you need all post objects, you can add these to the context:

class PostDetailView(DetailView):
    model = Post
    
    # …
    
    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context.update(
            texts=self.object.content,
            
        )
        return context

and render these as:

{% for  %}
    {{  }}
{% endfor %}

It might be better to work with an F expression [Django-doc] when incrementing the view counter to avoid race conditions :

class PostDetailView(DetailView): model = Post def get_object(self): obj = super().get_object() views = obj.view_count obj.view_count = obj.save() obj.view_count = return obj obj.save() obj.view_count =返回对象

first import models

from . models import Post

then in your function

data=Post.objects.values('content').all()

Now data have all the values in content field data=[{'content':first_value},{'content':second_value},..like this...]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM