implementing a counter that counts requests with django

拈花ヽ惹草 提交于 2019-12-10 17:54:10

问题


I'm just trying with Django, how can i implement a counter that stores the count of requests served on the database?

I want to count the GET requests, what should I do make it work?

my template,

<form action="/submit/" method="GET">
    <input type="text" name="q">
    <input type="submit" value="Submit">
</form>

my view

def result(request):
    name = request.GET['q']
    message = 'your name is %r ' % name
    return render(request, 'result.html', {'message': message})

I want to count the number of times i press the submit button. should I start a new app counter or there exist some other way to implement a counter?


回答1:


This might be a little messed up but you should get the basic concept you get it from the session not the get methods model/.py the model that tracts your friend

friend = models.ForeignKey("self", related_name="referral", null=True, blank=True) 

view.py this is how many friends you have counted

cool_obj = cool.objects.get(ref_id=ref_id)
obj = cool.objects.filter(friend=cool_obj)
count = cool_obj.referral.all().count()

this how you get your stuff through the middleware

def home(request):
try:
    cool_id = request.session['cool_id_ref']
    obj = cool.objects.get(id=cool_id)
except:
    obj = None 
form = CoolJoin(request.POST or None)
if form.is_valid():
    new_cool = form.save(commit=False)
    email = form.cleaned_data['email']
    new_cools, created = cool.objects.get_or_create(email=email)
    if created:
        new_cools.ref_id = get_ref_id()
        if not obj == None:
            new_cools.friend = obj
        new_cools.ip_adress = get_ip(request)
        new_cools.save()

    return HttpResponseRedirect("/%s" %(new_cools.ref_id))
context = {"form": form}
template = "home.html"
return render(request, template, context)

middleware/py.

class ReferMiddleware():
def process_request(self, request):
        ref_id = request.GET.get("ref")
        try:
            obj = cool.objects.get(ref_id = ref_id)
        except:
            obj = None   
        if obj:
            request.session['cool_id_ref'] = obj.id



回答2:


Well, session is just for this.

def result(request):
    name = request.GET['q']
    message = 'your name is %r ' % name
    hit = request.session.get('hit')
    if not hit:
        request.session['hit'] = 1
    else: 
        request.session['hit'] += 1
    return render(request, 'result.html', {'message': message})

With session, you can store information between requests.




回答3:


You should use some key-value storage for counter. For example, redis. It has built-in method INCR wich can be used for counters:

http://redis.io/commands/INCR

There are some redis-based django applications to store counters:

https://github.com/bradmontgomery/django-redis-metrics

https://github.com/Suor/django-counters

But I think it would be better to code counter by yourself, without any additional apps for django. There is python-redis driver:

https://pypi.python.org/pypi/redis/

As you can see from documentation it is pretty simple to interact with redis from python.



来源:https://stackoverflow.com/questions/28880133/implementing-a-counter-that-counts-requests-with-django

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!