weixin_33724570 2019-03-08 00:15 采纳率: 0%
浏览 40

模态不加载表格数据

I have the data form and the data update views, when I'm not using the modal, it works perfectly, however when using modal, it loads the empty forms, and if I load without the modal, the data saved in the database appear . I do not know where I'm going wrong. I can change the user name and save, the problem is that let's say user1 and I want to change to the user2 name, when I click to change the name, the form field does not come with user1 filled in. and if I do not use the modal, it comes with the user1 already filled in

views.py

class UserNameUpdate(LoginRequiredMixin, UpdateView):
    model= User 
    fields = ['username']
    template_name=' /change-username.html'

    def get_object(self, queryset=None):
        if queryset is None:
            queryset = self.get_queryset()   # This should help to get current user 

        # Next, try looking up by primary key of Usario model.
        queryset = queryset.filter(pk=self.request.user.usuario.pk)


        try:
            # Get the single item from the filtered queryset
            obj = queryset.get()
        except queryset.model.DoesNotExist:
            raise Http404("No user matching this query")
        return obj


    def get_success_url(self):
        return reverse('sistema_perfil')

change-username.html

{% load static %}
{% load bootstrap %}
{% load widget_tweaks %}
{% load crispy_forms_tags %}
{% block main %}
<div class="container">
<div class="card text-white bg-primary mb-3">
        <div class="card-header"><small class="text">
          <a href="{% url 'sistema_index' %}" class="text-white ">Home /</a>  
          <a href="{% url 'sistema_perfil' %}" class="text-white ">Perfil /</a>  
          <a  class="text-white">Usúario</a>
            </small></a></p> Alterar Nome do Usúario</div>
  <div class="card bg-light text-center ">
  <div class="card-body text-secondary text-center">



<form method="post" action="{% url 'sistema_change_username' %}" class="form-signin" enctype="multipart/form-data" id="form" name="form" validate>
{% csrf_token %}

<div class="form-row "> 


    <div class="form-group col-md-6 text-center">
        {{ form.username| as_crispy_field}}
      </div>
</div>

<input type="submit" class="btn btn-primary " >

</form>


<a class="btn btn-light float-left" href="sistema_perfil" role="button">Voltar</a>


</div>
</div>
</div>
</div>
{% endblock %}

menu_perfil.html

    <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal">
            Launch demo modal
        </button>

                    <!-- Modal -->
                    <div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
                        <div class="modal-dialog" role="document">
                        <div class="modal-content">
                            <div class="modal-header">
                            <h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
                            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                <span aria-hidden="true">&times;</span>
                            </button>
                            </div>
                            <div class="modal-body">

    {% include 'profile/change-username.html' %}
                            </div>
                            <div class="modal-footer">

                            </div>
                        </div>
                        </div>
                    </div>
  • 写回答

1条回答 默认 最新

  • ?Briella 2019-03-09 00:21
    关注

    Am I right that the with-modal and without-modal templates load from two different views? If so, the problem is how you are using Django's {% include %} template tag. Here are the docs:

    https://docs.djangoproject.com/en/2.1/ref/templates/builtins/#include

    Key line: "An included template is rendered within the context of the template that includes it."

    It looks like menu_perfil.html is rendered from a different view, which doesn't pass the form object into its context. So menu_perfil.html doesn't know what {{ form }} is. Whereas change-username.html is rendered with your UsernameUpdate view with the form object. Try moving your modal HTML into change-username.html, around your form code, instead of including a separate snippet, and see if that solves the problem.

    In general, inheriting from base templates using {% extends %} is preferable to using includes unless you really have to, because of limitations like this.

    评论

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧