计数器按钮django

时间:2017-03-23 15:31:08

标签: python-2.7 django-models

我有一个应用程序,我从头开始创建一个类似的按钮。我的模型在我的视图文件中有很多很多。我需要让计数器在我的html中发布已发布的次数。但我得到一个错误所以我知道代码行

counter=Mysecret.objects.annotate(var=Count('creator')).order_by('created_at') is my issue. Need somone to ell me a better way to code it so it displays on my home page. 

    Traceback:

    File "C:\Users\dbhol\Desktop\DojoAssignments\Python\myenvirnoments\djangoENv\lib\site-packages\django\core\handlers\exception.py" in inner
      42.             response = get_response(request)

    File "C:\Users\dbhol\Desktop\DojoAssignments\Python\myenvirnoments\djangoENv\lib\site-packages\django\core\handlers\base.py" in _get_response
      187.                 response = self.process_exception_by_middleware(e, request)

    File "C:\Users\dbhol\Desktop\DojoAssignments\Python\myenvirnoments\djangoENv\lib\site-packages\django\core\handlers\base.py" in _get_response
      185.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

    File "C:\Users\dbhol\Desktop\DojoAssignments\Python\myenvirnoments\django2\dojosecrets\dojosecrets\apps\secretdojo\views.py" in index
      9.    counter=Mysecret.objects.annotate(var=Count('creator')).order_by('created_at')

    Exception Type: NameError at /secretdojo/
    Exception Value: global name 'Count' is not defined


Html index

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
    <h1>Hello {{ request.session.user_fname }}!</h1>




    <form class="" action= method="post" >
            {% csrf_token %}
        <input type="submit" name="Logout" value="Logout">
    </form>




        <h2>Your Secret Here</h2>
        <form action="{% url 'secretdojo:addsecret' %}" name="secret1" method="post">
                {% csrf_token %}
            <textarea name="message" rows="8" cols="80"></textarea>
            <input type="submit" name="submit" value="post">
        </form>








         <h3>Most Popular Secrets</h3>
        <form class="" action="{% url 'secretdojo:index' %}" name="" method="post">
            {% csrf_token %}
        <input type="submit" name="Logout" value="Secret">
        </form>



            <h3>Recent Secrets</h3>


                 <table class="table">
                <tr>
                    <th>Id user</th>
                    <th>Id message</th>
                    <th>Course Name</th>
                    <th>Date Added</th>
                    <th>Action</th>
                </tr>
                {% for dog in secret %}
                <tr>

                    <td>{{ dog.secret }}</td>

                   {% for dog in counter %}
                    {{dog.var}}
                     {% endfor %}
                    <td>{{ dog.created_at }}</td>
                    {% if dog.creator.id == request.session.user_id %}
                    <td><form action="{% url 'secretdojo:remove' id=dog.id %}">{% csrf_token %}<input class="btn btn-default" type="submit" value="Remove"></form></td> 

                    <td><form action="{% url 'secretdojo:like' id=dog.id %}">{% csrf_token %}<input class="btn btn-default" type="submit" value="Like"></form></td> 
                      {% endif %}
                       {% endfor %}
                </tr>

            </table>

            Have secretes all list here Have date and time   and a like button if like button click increase count on post. Look into counter per post

            Add delete function to page

            need table for post  in table need spot for secrets. Need spot for like count  date time 





        </div>
</html>

    views.py 

    from django.shortcuts import render, redirect
    from . models import Mysecret
    from ..logReg.models import User


    # Create your views here.

    def index(request):
        counter=Mysecret.objects.annotate(var=Count('creator')).order_by('created_at')
        context = {
        "counter" : counter,
        "secret": Mysecret.objects.all(),


        }

        return render(request, 'secretdojo/index.html', context)


    def create(request):
        secreteid= User.objects.get(id=request.session['user_id'])

        Mysecret.objects.create( secret=request.POST['message'], creator=secreteid)

        return redirect( 'secretdojo:index')

    def removesecret(request, id):

        mainid = Mysecret.objects.get(id=id)
        userid =User.objects.get(id=request.session['user_id'])

        if mainid.creator != userid:
            return False
        mainid.delete()

        return redirect( 'secretdojo:index')

    def like(request, id):

        secreteid= User.objects.get(id=request.session['user_id'])
        mainid = Mysecret.objects.get(id=id)
        mainid.loguser.add(secreteid)

        return redirect( 'secretdojo:index')

    def topsecret(request):
        context = {


        }

        return redirect( '/')


    manage.py


    from __future__ import unicode_literals
    from django.db import models
    from ..logReg.models import User

    class Mysecret(models.Model):
        secret = models.CharField(max_length =500)
        created_at = models.DateTimeField(auto_now_add = True)
        updated_at = models.DateTimeField(auto_now = True)
        loguser = models.ManyToManyField(User, related_name='loguser')
        creator = models.ForeignKey(User, related_name='creator')

1 个答案:

答案 0 :(得分:0)

视图顶部的导入计数。是问题。