比较两个列表python django的值?

时间:2017-05-31 06:30:06

标签: python django

我有两个来自我的视图的列表,我正在比较它们并检查多选下拉列表中匹配的值。但逻辑似乎工作正常,但它往往会增加多选的数据。

 <div class="form-group">
    <label class="col-sm-4 control-label text_left">University <span class="text-danger">*</span></label>
    <div class="col-sm-8">
        <select class="form-control" value="university_all_list.id" name="universityId"
                id="universityName" required>
            <option>Select</option>

            {% for university_name in university_all_list %}
                {% for id in university_ids_list %}
                {% if  id == university_name.id %}
                    <option value="{{ university_name.id }}"
                            selected>{{ university_name.name }}</option>
                {% else %}
                    <option value="{{ university_name.id }}">{{ university_name.name }}</option>
                {% endif %}
            {% endfor %}
            {% endfor %}
        </select>
    </div>
</div>

任何解决方案,它都不会在下拉列表中乘以我的数据。我知道我正在使用多个for循环,因为这样,但可能是解决方案。

我的观点:

@login_required
@csrf_exempt
@cms_user_edit_decorator
def admin_user_edit(request, cms_user_id=None):
    university_ids_list = []
    university_ids ={}
    university_all_list = Universities.objects.using('cms').all()
    master_cms_user_type_list = MasterCmsUserTypes.objects.all()
    cms_user = CmsUser.objects.get(userId=cms_user_id)
    school_university_mapping = SchoolAdminUniversityMappings.objects.filter(userId=cms_user)
    for university_ids in school_university_mapping:
        university_ids_list.append(university_ids.universityId)
    print university_ids_list
   # zip_list = zip(A, cycle(B)) if len(A) > len(B) else zip(cycle(A), B)
    user_obj = {}
    user_showing = 'edit'
    if cms_user_id is not None:
        user_showing = 'edit'
        user_obj = User.objects.get(id=cms_user_id)
        if request.method == 'POST':
            first_name = request.POST.get('firstName').strip()
            last_name = request.POST.get('lastName').strip()
            username = request.POST.get('username').strip()
            email = request.POST.get('email').strip()
            status = request.POST.get('status')
            university = request.POST.getlist('universityId')
            print 'hulu lulu'
            print university
            staff = False
            superUser = False
            if status is None or status == '':
                status = False
            else:
                status = True
            if user_obj.email == email:
                if user_obj.username == username:
                    if cms_user_id:
                        user_obj = User.objects.get(id=cms_user_id)
                        user_obj.first_name = first_name
                        user_obj.last_name = last_name
                        user_obj.email = email
                        user_obj.username = username
                        user_obj.is_active = status
                        user_obj.is_staff = staff
                        user_obj.is_superuser = superUser
                        user_obj.save()
                        messages.success(request, 'Successfully update to the Admin User')
                        return redirect('cms:admin-user-list')
                else:
                    if User.objects.filter(username=username).exists():
                        messages.error(request, 'username already registered')
                    elif cms_user_id:
                        user_obj = User.objects.get(id=cms_user_id)
                        user_obj.first_name = first_name
                        user_obj.last_name = last_name
                        user_obj.email = email
                        user_obj.username = username
                        user_obj.is_active = status
                        user_obj.is_staff = staff
                        user_obj.is_superuser = superUser
                        user_obj.save()
                        messages.success(request, 'Successfully update to the Admin User')
                        return redirect('cms:admin-user-list')
            else:
                if User.objects.filter(email=email).exists():
                    messages.error(request, 'email already exists')
                elif cms_user_id:
                    user_obj = User.objects.get(id=cms_user_id)
                    user_obj.first_name = first_name
                    user_obj.last_name = last_name
                    user_obj.email = email
                    user_obj.username = username
                    user_obj.is_active = status
                    user_obj.is_staff = staff
                    user_obj.is_superuser = superUser
                    user_obj.save()
                    messages.success(request, 'Successfully update to the Admin User')
                    return redirect('cms:admin-user-list')
    return render(request, 'templates/admin_user_management/admin_user_edit.html', {
        'user_obj': user_obj,
        'user_showing': user_showing, 'master_cms_user_type_list': master_cms_user_type_list,
        'cms_user': cms_user,'university_all_list':university_all_list,'university_ids_list':university_ids_list,'list':list
    })

0 个答案:

没有答案