小编Ala*_*air的帖子

Git - 如何将本地存储库提交到另一个本地存储库的子文件夹?

我有一个我刚刚开始的Django项目,我在bitbucket上主持它.现在我需要在openshift中托管它,并且这样做的方法是它们为您提供了一个git存储库,并且每次推送它们都会自动部署.问题是存储库附带了几个用于配置和设置的顶级文件夹,而有效的django项目必须位于名为wsig/openshift的子文件夹中.

我的问题是,如何将我的本地django存储库中的更改提交到本地openshift存储库的wsig/openshift子文件夹?因为我打算继续在bitbucket /本地存储库上开发

git django openshift

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

Django类覆盖失败系统检查

我想在我的开发环境中从Django 1.7.1升级到1.8.我似乎遇到了我的一个模型的问题,我认为核心文件已经升级并且弄乱了我的模型.我似乎无法弄清楚是什么导致它死亡.

这是我尝试运行manage.py测试时遇到的唯一错误

CommandError: System check identified some issues:

ERRORS:
graphite_alerts.CheckResults: (models.E020) The 'CheckResults.check()' class method is currently   overridden by <django.db.models.fields.related.ReverseSingleRelatedObjectDescriptor object at 0x3a76310>.
Run Code Online (Sandbox Code Playgroud)

我尝试更改类名并查看我的模型,似乎无法找到任何会导致覆盖错误的内容

这是班级:

class CheckResults(models.Model):

    WARN = 'warn'
    ERROR = 'error'
    OK = 'ok'
    DOWN = 'down'
    STATUS_CHOICES = (
        (WARN, 'Warn'),
        (ERROR, 'Error'),
        (OK, 'OK'),
        (DOWN, 'Down'),
    )

    target = models.CharField(max_length=1000)
    additional_graph_target = models.CharField(max_length=1000, blank=True)
    value = models.DecimalField(max_digits=9, decimal_places=2)
    timestamp = models.DateTimeField(db_index=True)
    status = models.CharField(max_length=6, choices=STATUS_CHOICES, default='ok')
    check = models.ForeignKey(Check, related_name='results')
    tags = TaggableManager()

    def __unicode__(self): …
Run Code Online (Sandbox Code Playgroud)

python django overriding models

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

Django 1.8:不能在查询中使用查询集

我正在尝试这样做:

wider_circle = # some queryset
friends_you_may_know = list(wider_circle.exclude(user_id__in=user.connections))
Run Code Online (Sandbox Code Playgroud)

但我收到此错误:

RemovedInDjango19Warning: Passing callable arguments to queryset is deprecated
Run Code Online (Sandbox Code Playgroud)

它适用于 Django 1.6,但在 1.8 上引发错误

谢谢 :)

django django-queryset django-1.8

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

在Django REST中存储BooleanField()和IntegerField()的默认值

我知道根据文档,以下字段不采取allow_blank=Trueallow_null=True

BooleanField()
IntegerField()
Run Code Online (Sandbox Code Playgroud)

我需要允许客户端不指定g或d(如下所示)并将值存储在DB中None.

g = serializers.BooleanField()
d = serializers.IntegerField()
Run Code Online (Sandbox Code Playgroud)

有任何想法吗 ?

django django-rest-framework

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

Django assertTemplateUsed()引发Jinja模板异常

当我尝试运行此测试时:

from django.test import TestCase
from django.core.urlresolvers import reverse
from django.test import Client

class StatisticTest(TestCase):
    def setUp(self):
        self.client = Client()

    def test_schedule_view(self):
        url = reverse('schedule')
        response = self.client.get(url)
        self.assertEqual(response.status_code, 200)
        self.assertTemplateUsed(response, 'schedule.html')
Run Code Online (Sandbox Code Playgroud)

我得到AssertionError:没有用于呈现响应的模板。

我的看法是:

class Schedule(View):
    def get(self, request):
        games = add_team_info(query.get_current_schedule())
        if games is not []:
             available_schedules = generate_schedule_list(games[0]["season_type"], games[0]["week"])
             is_available = True
        else:
             available_schedules = []
             is_available = False
        return render_to_response("schedule.html", 
                              {"games": games, "available_schedules": available_schedules, "is_available": is_available})
Run Code Online (Sandbox Code Playgroud)

和urls.py:

url(r'^schedule/$', views.Schedule.as_view(), name='schedule'),
Run Code Online (Sandbox Code Playgroud)

python testing django assert jinja2

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

Django API Post方法返回403错误

我正在尝试设置Django API(POST API端点).我希望有相同的URL路径指向同一个处理不同的函数,因为它是POST或GET.因此,我使用了这样的方法

def handle_post(request):

    dict = {}
    dict['email'] = "test"

    if request.method == "POST":
        return HttpResponse(json.dumps(dict), content_type="application/json")
Run Code Online (Sandbox Code Playgroud)

在url.py中,我有以下代码

router = routers.DefaultRouter()
router.register(r'notes', UsernotesViewSet)
urlpatterns = patterns('',
url(r'^', include(router.urls)),
url(r'^admin/', include(admin_site.urls)),
url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')),
url(r'^docs/', include('rest_framework_swagger.urls')),
url(r'^example/postrequest', handle_post),
)
Run Code Online (Sandbox Code Playgroud)

但是当我在URL http://127.0.0.1:8000/example/postrequest?requestid=abc&starthour=10上执行POST时,我无法完成这项工作 .我没有发布任何内容,只是在httpclient上将方法从GET更改为POST以尝试此API.如果我没有将任何内容发布到URL,这样可以吗?

我收到403错误,如下所示:

禁止(403)
CSRF验证失败.请求中止.
您看到此消息是因为此站点在提交表单时需要CSRF cookie.出于安全原因,需要此cookie,以确保您的浏览器不会被第三方劫持.如果您已将浏览器配置为禁用cookie,请重新启用它们,至少对于此站点或"同源"请求.

感谢任何帮助.

python django http-post http-status-code-403

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

Python - Django - 将参数传递给表单干净方法

我正在尝试使用__init__参数将变量传递给 ModelForm clean 方法,但到目前为止还没有成功 - 我查看了 StackOverflow 上的各种帖子,但似乎没有一个有帮助。

我的代码如下:

forms.py

class property_booking_form(forms.ModelForm):
    check_in_date = forms.DateField(widget=SelectDateWidget)
    check_out_date = forms.DateField(widget=SelectDateWidget)

    class Meta:
        model = Properties_bookings
        fields = ['check_in_date', 'check_out_date']

    def __init__(self, property_id):
        self.property_id = property_id
        super(property_booking_form, self).__init__(self, property_id)

    def clean(self):
        check_in_date = self.cleaned_data.get('check_in_date')
        check_out_date = self.cleaned_data.get('check_out_date')
        property_min_nights = Properties.objects.get(id=self.property_id).property_minimum_nights
        ...

views.py

def view(request):
    ...
    if request.method == 'POST':
        booking_form = property_booking_form(request.POST, property_id=property_id)
        if booking_form.is_valid():
            ...
    else:
        booking_form = property_booking_form(property_id=property_id)
    return render(...)
Run Code Online (Sandbox Code Playgroud)

这会引发以下错误:“property_booking_form”对象没有属性“get”

根据错误描述,这似乎与小部件有关:

异常位置:

/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/forms/widgets.py in value_from_datadict, line 1058
Run Code Online (Sandbox Code Playgroud)

该表单在没有覆盖的情况下工作正常 …

python django django-forms django-validation

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

django.db.utils.OperationalError:“ []”附近:语法错误

使用makemigrations命令后,出现此错误,我尝试为此注释不同的列,但无法正常工作

    C:\Users\Rushabh\Desktop\project\MyPrj>python manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, paper, sessions
Running migrations:
  Applying paper.0014_auto_20170405_1549...Traceback (most recent call last):
  File "C:\Users\Rushabh\Anaconda3\lib\site-packages\django\db\backends\utils.py", line 62, in execute
    return self.cursor.execute(sql)
  File "C:\Users\Rushabh\Anaconda3\lib\site-packages\django\db\backends\sqlite3\base.py", line 335, in execute
    return Database.Cursor.execute(self, query)
sqlite3.OperationalError: near "[]": syntax error

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "C:\Users\Rushabh\Anaconda3\lib\site-packages\django\core\management\__init__.py", line 367, in execute_from_command_line
    utility.execute()
  File "C:\Users\Rushabh\Anaconda3\lib\site-packages\django\core\management\__init__.py", line 359, in …
Run Code Online (Sandbox Code Playgroud)

django django-migrations

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

django多对多形式显示人类可读

我有以下模型:

CURSES=(('python','Python'),('django','Django'),...)
class Asig(models.Model):
    ...
    name = models.CharField(max_length=100, choices=CURSES)

class Profesor(AbstractUser):
    ...
    asigs = models.ManyToManyField(Asig)
Run Code Online (Sandbox Code Playgroud)

然后,当我使用ModelForm渲染表单时,多对多字段使用'python'字符串而不是'Python'来显示自身,此外,当我查看呈现的html编码时,multiselect选项如下所示:

<option value='1'>python</option>
Run Code Online (Sandbox Code Playgroud)

代替

<option value='python'>Python</option> 
Run Code Online (Sandbox Code Playgroud)

django many-to-many

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

Django Template:key,for循环中不可能的值

我得到的错误:

需要2个值来解包for循环; 得到1.

这是我的观点:

class Index(View):
    def get(self, request, slug):
        test = {
            1: {
                'id': 1,
                'slug': 'test-slug-1',
                'name': 'Test Name 1'
            },
            2: {
                'id': 2,
                'slug': 'test-slug-2',
                'name': 'Test Name 2'
            }
        }
        context = {
            'test': test
        }
        return render(request, 'wiki/category/index.html', context)
Run Code Online (Sandbox Code Playgroud)

这是我的模板:

{% block content %}
    <div>
        {{ test }}
        <ul>
            {% for key, value in test %}
                <li>
                    <a href="#">{{ key }}: {{ value }}</a>
                </li>
            {% endfor %}
        </ul> …
Run Code Online (Sandbox Code Playgroud)

python django django-templates django-views

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