Django无法根据链接点击更新模型

西瑞思

我试图在从服务器下载文件时更新文件的模型记录。

我目前正在使用一个自定义标记来查找适当的记录并将“已使用”布尔值更改为 True,但是我现在意识到这是在呈现链接时发生的,而不是在单击时发生。

很明显这不是我应该这样做的方式,但我的搜索没有找到替代方法。任何帮助将不胜感激!

个人资料.html

{% for x in source %}
    {% if forloop.counter <= 4 %}
      <div class="content-section">
        <div class="media-body">
        <h2 class="account-heading">{{ x.video_id }}</h2>
        {% for y in records %}
          {% if x.video_id == y.sourcevideo.video_id %}
            <div class="media-body">
              <video width="320" height="240" controls>
                <source src="{{ y.highlight.url }}" type="video/mp4">
                  Your browser does not support the video tag
                </video>
                <h1>{{ y.highlight_id }}</h1>
                <br>
                <a class="btn" href="{% downloaded y.highlight.url y.highlight_id %}" download>Download</a>
            </div>
          {% endif %}
        {% endfor %}
        </div>
      </div>
    {% endif %}
  {% endfor %}

模板标签/analytics.py

from django import template
from highlights.models import Highlight


register = template.Library()


@register.simple_tag
def downloaded(url, pk):
    e = Highlight.objects.get(highlight_id=pk)
    e.used = True
    e.save()
    return url

来自views.py

class ProfileView(ListView):
    model = Highlight
    template_name = 'users/profile.html'
    context_object_name = 'records'
    ordering = ['-date_created']

    @method_decorator(login_required)
    def dispatch(self, *args, **kwargs):
        return super().dispatch(*args, **kwargs)

    def get_queryset(self):
        return Highlight.objects.filter(creator=self.request.user)

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['source'] = Video.objects.filter(creator=self.request.user).order_by('-pk')
        return context
quqa123

这正是 Ajax 的用途。就像“当用户单击按钮时,触发我的后端功能”。而不是仅仅为了处理而创建整个视图,您只需定义具有 JSONResponse 作为返回的函数,您就可以开始了。您了解它后,它将改善您的 django/web 开发体验。这是链接

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章