小编H C*_*H C的帖子

在bootstrap页面上居中表

我试图使用bootstrap中心表.

这是html:

<div class="container-fluid">
    <table id="total votes" class="table table-hover text-centered">
        <thead>
            <tr>
                <th>Total votes</th>
                <th> = </th>
                <th>Voter A</th>
                <th> + </th>
                <th>Voter B</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>{{ total_votes }}</td>
                <td> = </td>
                <td>{{ total_voter_a }}</td>
                <td> + </td>
                <td>{{ total_voter_b }}</td>
            </tr>
        </tbody>    
    </table>
</div>
Run Code Online (Sandbox Code Playgroud)

但是,无论我如何调整css,桌子仍然与左边对齐.我确定我错过了一些简单的事情.我还以为container-fluid会让这个表跨越整个页面.

这是css:

.table th {
    text-align: center;
}

.table {
    border-radius: 5px;
    width: 50%;
    float: none;
}
Run Code Online (Sandbox Code Playgroud)

html css twitter-bootstrap

12
推荐指数
2
解决办法
6万
查看次数

在AJAX调用之后如何更新django模板上下文变量?

我有一个表格产品,显示一组产品的信息.

    <table id="item_table" class="table table-sm table-hover table-bordered">
        <thead class="thead-inverse">
        <tr>
            <th colspan="2">Date</th>
            <th colspan="6">Product name</th>
            <th colspan="2">Category</th>
            <th colspan="2">Amount</th>
        </tr>
        </thead>
        <tbody>
            {% for item in product_list %}
            <tr>
                <td colspan="2">{{ item.date }}</td>
                <td id="item_name_format" colspan="6">{{ item.name }}</td>
                {% if item.category_id %}
                <td id="item_name_format" colspan="2">{{ item.category_id.level1_desc }}</td>
                {% endif %}
                <td id="item_amt_format" colspan="2">${{ item.amount|intcomma }}</td>
            </tr>
            {% endfor %}
        </tbody>
    </table>
Run Code Online (Sandbox Code Playgroud)

我正在使用下面的Ajax调用更新表.

$(document).ready(function(){

// Submit post on submit
$('.item_num').on('click', function(event){
    event.preventDefault();
    var item_num = $(this).attr('id');
    update_item(item_num);
});

function update_item(item_num) { …
Run Code Online (Sandbox Code Playgroud)

django ajax jquery django-templates

10
推荐指数
1
解决办法
9004
查看次数

为什么"模型尚未加载"?

我正在尝试使用customUser安装django-registration-redux.

我已将其包含在我的settings.py中:

AUTH_USER_MODEL = 'app.customUser'
Run Code Online (Sandbox Code Playgroud)

注册表位于目录../registration/forms.py中:

from __future__ import unicode_literals


from django import forms
from django.utils.translation import ugettext_lazy as _
from django.contrib.auth.forms import UserCreationForm

from .users import UserModel, UsernameField

User = UserModel()


class RegistrationForm(UserCreationForm):

    required_css_class = 'required'
    email = forms.EmailField(label=_("E-mail"))

    class Meta:
        model = customUser
        fields = ('email')
Run Code Online (Sandbox Code Playgroud)

此外,models.py还具有以下内容:

from __future__ import unicode_literals
from django.db import models

# Create your models here.

import datetime
import hashlib
import random
import re

from django.conf import settings
from django.core.mail import EmailMultiAlternatives
from django.db …
Run Code Online (Sandbox Code Playgroud)

python django django-apps

8
推荐指数
1
解决办法
5303
查看次数

如何只显示Django序列化器中的值?

我正在使用'rest_serializer'模块实现Django REST API框架:

当前输出为:

{
    "count": 86,
    "next": "http://127.0.0.1:8000/state/?page=2",
    "previous": null,
    "results": [
        {
            "state_name": "Alaska"
        },
        {
            "state_name": "California"
        },
        ...
     ]
}
Run Code Online (Sandbox Code Playgroud)

如何将其显示为json列表:

[
     "Alaska",
     "California",
     ...
]
Run Code Online (Sandbox Code Playgroud)

以下是我的序列化器:

from .models import States
from rest_framework import serializers


class StateSerializer(serializers.ModelSerializer):
    class Meta:
        model = State
        fields = ('state_name',)
Run Code Online (Sandbox Code Playgroud)

view.py

class StateViewSet(viewsets.ModelViewSet):
    """
    API endpoint that allows groups to be viewed or edited.
    """
    queryset = States.objects.values('state_name').distinct();
    serializer_class = StateSerializer
Run Code Online (Sandbox Code Playgroud)

django django-serializer django-rest-framework

5
推荐指数
2
解决办法
1985
查看次数

AttributeError:“UserManager”对象没有属性“create_superuser”

我按照 Django 的方式设置了自定义用户管理器,但仍然收到属性错误。我不确定还有什么问题。

class UserManager(BaseUserManager):
    def create_user(self, email, date_of_birth, password=None):
        """
        Creates and saves a User with the given email, date of
        birth and password.
        """
        if not email:
            raise ValueError('Users must have an email address')

        user = self.model(
            email=self.normalize_email(email),
        )

        user.set_password(password)
        user.save(using=self._db)
        return user

    def create_superuser(self, email, date_of_birth, password):
        """
        Creates and saves a superuser with the given email, date of
        birth and password.
        """
        user = self.create_user(email,
            password=password,
            date_of_birth=date_of_birth
        )
        user.is_admin = True
        user.save(using=self._db)
        return user

class User(AbstractBaseUser): …
Run Code Online (Sandbox Code Playgroud)

django

4
推荐指数
1
解决办法
2万
查看次数

查询中的min函数设置Django以查找模型中的较早日期?

我有一个模型,每个条目都有一个日期.如何使用queryset查找具有最早日期的条目?这不依赖于'pk',因为我可以在以后添加一个具有更早日期的行.

这是我到目前为止,它似乎没有工作:

Model.objects.get(min(date))
Run Code Online (Sandbox Code Playgroud)

谢谢.

django django-models django-queryset

4
推荐指数
2
解决办法
3134
查看次数

在 Django 的 loaddata 中设置外键

我设置了两个类,其中一个用户可能有多个访问密钥。

class User(models.Model):
    first_name = models.CharField(max_length=50)
    middle_name = models.CharField(max_length=50)
    last_name = models.CharField(max_length=50)
    email = models.EmailField()
    password = models.CharField(max_length=50)
    birthday = models.DateField()

    def __str__(self):
        return self.first_name+" "+self.last_name

class pets(models.Model):
    user = models.ForeignKey('User')
    type = models.CharField(max_length=50)
    color = models.CharField(max_length=50)

    def __str__(self):
        return self.type
Run Code Online (Sandbox Code Playgroud)

我正在尝试使用 loaddata 通过如下所示的 json 文件预加载包含数据的表:

[
{
    "fields": {
        "first_name": "John",
        "last_name": "Doe",
        "middle_name": "G",
        "birthday": "1900-07-21",
        "password": "goforit123",
        "email": "John.Doe@gmail.com"
        },
        "model": "account_data.user",
    "pk": 1
},
{
    "fields": {
        "user": "????"
        "type": "dog",
        "color": "blue"
    },
    "model": "account_data.pets", …
Run Code Online (Sandbox Code Playgroud)

django json django-models python-2.7

3
推荐指数
1
解决办法
1678
查看次数

Django Queryset过滤列表中的参数

我有一个名单:

name_list = ['John', 'Bill', 'Charlie']
Run Code Online (Sandbox Code Playgroud)

我想过滤name字段匹配任何这些名称的结果.如何在queryset中实现它?我使用的代码是:

Special_group = People.objects.filter(name=name_list(?))
Run Code Online (Sandbox Code Playgroud)

谢谢.

python django django-queryset

3
推荐指数
1
解决办法
1344
查看次数

在多个 django 模板中重用相同的 html“块”

目前,我有两个从 base.html 扩展的 html 模板:

page1.html:

{% extends 'dashboard/base.html' %}
{% block tittle %} Dashboard1 {% endblock %}
... code ...
Code_block_1
{% endblock %}
Run Code Online (Sandbox Code Playgroud)

page2.html:

{% extends 'dashboard/base.html' %}
{% block tittle %} Dashboard2 {% endblock %}
... code ...
Code_block_1
{% endblock %}
Run Code Online (Sandbox Code Playgroud)

两个 html 共享相同的 Code_block_1。

我正在考虑创建另一个名为 Code_block_1.html 的 html 来合并这段重复的代码。然后,将 Code_block_1.html 插入到 page1.html 和 pag2.html 中。Django 只允许你扩展一次。我该如何解决这个问题?

谢谢。

html django django-templates

3
推荐指数
1
解决办法
1495
查看次数

将 Json 转换为数组,仅提取值

我有一个 JSON 文件:

['0': XXX, '1': YYYY]
Run Code Online (Sandbox Code Playgroud)

我想通过 jquery 将它提取到一个只有值的数组中。

['XXX', 'YYYY']
Run Code Online (Sandbox Code Playgroud)

做这个的最好方式是什么?

.makeArray 似乎不起作用。

javascript jquery

3
推荐指数
1
解决办法
3715
查看次数

如何在 for 循环中排除查询集中的项目?

我想删除

results = Model.objects.filter(condition - satisfied... etc.)
Run Code Online (Sandbox Code Playgroud)

然后我遍历查询集并在我浏览代码时删除每个对象:

for a in results:

### code ###

    results.exclude(id=a.id)
Run Code Online (Sandbox Code Playgroud)

无论我如何排除,结果对象都不会改变。当我遍历查询集时,有没有办法“弹出”它们?

django django-models django-queryset

2
推荐指数
1
解决办法
1835
查看次数

如何更改为字符串并删除&#39; 从Django的tempalates /上下文?

我目前有一个采用Unicode格式的日期:

k = u'2015-02-01'
Run Code Online (Sandbox Code Playgroud)

我试图将其添加到列表并将其更改为字符串:

date = []
date.append(str(k))
Run Code Online (Sandbox Code Playgroud)

然后,我想将此作为Django上下文传递给我的模板。

但是,日期显示如下:

&#39;2015-02-01&#39;
Run Code Online (Sandbox Code Playgroud)

我如何摆脱$#39;并用双引号(“)替换?

非常感谢。

django json django-templates django-views

1
推荐指数
1
解决办法
2201
查看次数

如何为未知属性设置Django模型架构?

我有一个人物模型。

class Person(models.Model):
    name_first = models.CharField(max_length=100)
    name_middle = models.CharField(max_length=100, null=True, blank=True)
    name_last = models.CharField(max_length=100)
    date_birth = models.DateField(null=True, blank=True)
    date_death = models.DateField(null=True, blank=True)
Run Code Online (Sandbox Code Playgroud)

我正在尝试将其扩展到音乐界的不同角色:作曲家,表演者和赞助人。

一个人可以是一个,两个或所有三个角色。如果某人是表演者,我还需要为该人分配一种或多种乐器。在实例化时,您可能不知道某人是否是表演者。否则他们的角色会随着时间而改变。

我希望能够搜索并显示一个人(如果他是三个人)既是作曲家,钢琴家又是赞助人。例如:贝多芬是指挥,作曲家和钢琴家。

我对实现的最初想法是继承Person类。

class Composer(Person):
    pass

class Performer(Person):
    instrument = models.ManyToManyField(Instrument, verbose_name=_('instrument'), blank=True,)

class Patron(Person):
    pass

class Instrument(models.Model):
    name = models.CharField(max_length=100, null=True, blank=True)
Run Code Online (Sandbox Code Playgroud)

问题1:我应该改用某种抽象模型吗?如果是这样,我将如何处理?

问题2:我如何寻找一个人,并知道他们是否是作曲家,赞助人和/或表演者,以及他们是哪种表演者。

谢谢。

django django-models django-queryset

0
推荐指数
1
解决办法
174
查看次数