小编vmo*_*eco的帖子

Django:从开发服务器获取用户IP

我正在尝试将用户 IP 放在我发送给用户的页面中。我试过像这里解释的那样。

所以我的观点是:

def yourip(request):
    return HttpResponse("Your IP is : %s" % request.META.get('HTTP_X_FORWARDED_FOR'))
Run Code Online (Sandbox Code Playgroud)

request.META.get('HTTP_X_FORWARDED_FOR')返回None。是否可以从开发服务器执行此操作?还是其他地方的问题?

谢谢你。

python django

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

MutationObserver disconnect() 在回调函数中调用

我正在使用 MutationObserver 等待在页面上创建一个元素,然后在其上添加一个按钮(使用init函数)。但我只需要添加一个按钮,此后突变不断发生。disconnect()添加此按钮后,我想要观察者。

我试过这样的事情:

function detect_node_for_buttons(mutations){
    var selector = 'div[class="_2o3t fixed_elem"]';
    mutations.forEach(function (mutation){
        var element =   $(document).find(selector);
        if (element){
            init();
            observer.disconnect();
            return;
        }
        if (!mutation.addedNodes) return;
        for (var i = 0; i < mutation.addedNodes.length; i++){
            if (mutation.addedNodes[i].matches(selector)){
                init();
                observer.disconnect();
            }
        }
    });
}

var observer = new MutationObserver(function (mutations){
    detect_node_for_buttons(mutations);
});
Run Code Online (Sandbox Code Playgroud)

但它不起作用(也许是因为当我在detect_node_for_buttons() 中调用observer.disconnect()时尚未定义观察者)?

我怎么办?

javascript mutation-observers

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

如何避免在 zsh for 循环中使用空格作为分隔符?

我正在尝试制作一个小脚本来转换音乐库中的一些文件。

但如果我做类似的事情:

#!/usr/bin/zsh                                                                     

for x in $(find -name "*.m4a");
do
    echo $x;
done
Run Code Online (Sandbox Code Playgroud)

在包含以下内容的文件夹中进行解释时:

foo\ bar.m4a
Run Code Online (Sandbox Code Playgroud)

它会返回:

foo
bar.m4a
Run Code Online (Sandbox Code Playgroud)

如何防止 for 循环将空格字符解释为分隔符?

我可以替换$(find -name "*.m4a")$(find -name "*.m4a" | sed "s/ /_/g")然后sed在循环中使用另一种方式,但是如果文件名/路径已经包含下划线(或者我可以使用其他字符代替下划线)怎么办?

任何的想法?

for-loop zsh sh separator

3
推荐指数
2
解决办法
1292
查看次数

Django 没有检测到我尝试创建的模型

我(再次)从 django (1.7.5) 开始。

我创建了:

  • 一个名为“催眠”的项目。
  • 一个名为“ main ”的应用程序。
  • 一个名为“ Article ”的模型。
  • 超级用户。

我已经完成了足够的代码来使我的应用程序正常工作,我可以打开管理面板,浏览页面...但是当我尝试创建数据库时,django 未检测到文章模型(我尝试过使用syncdb, migrate 和 makemigrations) :Django 的行为就像模型根本不存在一样!我真的不知道问题来自哪里,也不知道它是否来自于我忘记的东西,或者是否与django 1.7的新功能有关(所有迁移的东西我都不太明白) )。

这是我的源文件:

主要/模型.py:

from django.db import models

# Create your models here.

class Article(models.Model):
    title = models.CharField(max_length = 100)
    content = models.TextField(null = True)
    date = models.DateTimeField(auto_now_add = True, auto_now = False,
    verbose_name = "Date de parution")
    slug = models.SlugField(max_length = 50)

    def __unicode__(self):
        return (u"%s" % (self.title,))

    def __str__(self):
        return ("%s" % (self.title,))
Run Code Online (Sandbox Code Playgroud)

主/管理.py:

from django.contrib import …
Run Code Online (Sandbox Code Playgroud)

django django-models django-syncdb

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

apache + wsgi + django:服务器配置拒绝客户端

我的django项目出现403错误.

我在错误日志中得到的唯一一行是:

AH01630: client denied by server configuration: /srv/http/www/src/myproject/preprod_wsgi.py
Run Code Online (Sandbox Code Playgroud)

该文件的权限是:

-rwxr-xr-x 1 root root  552 Jul 30 04:24 preprod_wsgi.py
Run Code Online (Sandbox Code Playgroud)

而apache(VERSION:2.4)conf文件包含(除其他外):

LoadModule wsgi_module modules/mod_wsgi.so

<IfModule unixd_module>
    User http
    Group http
</IfModule>

<Directory />
    AllowOverride none
    Require all denied
</Directory>

#
# Others
#

Alias /robots.txt /srv/http/www/htdocs/static/robots.txt
Alias /favicon.ico /srv/http/www/htdocs/static/favicon.ico


Alias /media/ /srv/http/www/htdocs/media/
Alias /static/ /srv/http/www/htdocs/static/


<Directory "/srv/http/www/htdocs/static">
           Require all granted
</Directory>

<Directory "/srv/http/www/htdocs/media">
           Require all granted
</Directory>

<Files "/srv/http/www/src/myproject/preprod_wsgi.py">
       Require all granted
</Files>

#
# WSGI
#

WSGIDaemonProcess myproject …
Run Code Online (Sandbox Code Playgroud)

python apache django mod-wsgi http-status-code-403

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

sdl,sdl2错误:SDL_window(等等)未声明

我已经玩C++了一段时间,最近刚开始进入SDLSDL2.

我能够让点演示程序工作.

但其他程序,如Lazy Foo'Productions的复制和粘贴似乎不起作用.我安装了SDL和SDL2(并且已卸载并重新安装.)我在Ubuntu 15.04上并且我有IDE CodeBlocks链接(-ISDL2)错误是SDL_Window- SDL_WINDOWPOS_UNDEFINED- SDL_WINDOW_SHOWN- SDL_CreateWindow- SDL_GetWindowSurface- SDL_UpdateWindowSurface最后,SDL_DestroyWindow- 未在此范围内声明.

另外,我包括:

#include </usr/include/SDL/SDL.h>
#include </usr/include/SDL2/SDL.h>
#include <stdio.h>
Run Code Online (Sandbox Code Playgroud)

我很确定我不需要所有这些位置,但如果没有它也无法工作.另外一点,当我输入时#includes,CodeBlocks会建议SDL2/SDL.h但不是SDL/SDL.h.

我错过了什么?

我不认为我可以在这里放入Lazy Foo代码 - 我没有得到许可......

c++ linux sdl codeblocks sdl-2

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

Python - 为多个父类调用 __init__

编辑:我在Python 3 上

我有一个类X扩展两个YZ

class X(Y, Z):
    def __init__(self):
    # Call Y's __init__
    # Call Z's __init
    pass
Run Code Online (Sandbox Code Playgroud)

我需要调用__init__两个YZ来自__init__X。什么是最好的方法来做到这一点?

python-3.x

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

是否可以列出存储在组中的频道?

是否可以使用django-channels访问添加到组中的频道列表?

def consumer(message):
    Group("group_name").add(message.reply_channel)
    channel_list = Group("group_name").???
Run Code Online (Sandbox Code Playgroud)

编辑:

我试图实现的目标是访问所有通道的会话,例如已连接用户的列表。

到目前为止,我使用列出连接的基于数据库的系统。但是,如果服务器在不执行ws_disconnect使用者的情况下关闭,则这些连接对象将保留在数据库中,而我不希望这样做。

编辑2:

要列出连接的用户,我发现django-channels-presence。我会测试。

django django-channels django-channels-presence

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

是否可以从不存在的实例调用方法?

我正在处理的代码:

我有以下代码(有关main.cpp中索引的错误):

Sample.hpp:

#ifndef SAMPLE_HPP
# define SAMPLE_HPP

# include <iostream>
# include <string>

class Sample{
public:
    Sample(void);
    ~Sample(void);
    void    tellname(void) const;
private:
    std::string     _name;
};

#endif
Run Code Online (Sandbox Code Playgroud)

Sample.cpp:

#include <iostream>
#include "Sample.hpp"

Sample::Sample(void){
    this->_name = "testname";
    return;
};

Sample::~Sample(void){
    return;
}

void    Sample::tellname(void) const{
    std::cout << "Name : " << this->_name << std::endl;
    return;
}
Run Code Online (Sandbox Code Playgroud)

main.cpp中

#include "Sample.hpp"

int     main(void){
    int     i;
    Sample  *test;

    test = new Sample[4];
    i = 0;
    while (i++ < 4)          // I know …
Run Code Online (Sandbox Code Playgroud)

c++

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

找到字符串中子串的小出现

我试图用re(括号内容)解析一些数学表达式.

我试过这个:

>>> re.compile("\(.*\)").findall("(1 + 2) + (3 + 4)")
['(1 + 2) + (3 + 4)']
Run Code Online (Sandbox Code Playgroud)

但我发现只有最大的发生,包括最后和第一个括号之间的内容.但我想只得到最小的.

我怎么能实现这个目标?

>>> re.compile(<expr>).findall("(1 + 2) + (3 + 4)")
['(1 + 2)', '(3 + 4')]
Run Code Online (Sandbox Code Playgroud)

我试过,以取代<expr>"\((?![\(\)])+\)"(用括号内括号排除出现次数),但它似乎并没有工作.

python regex

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

为什么修改 AUTH_USER_MODEL 似乎需要子类 AbstractUser?

创建项目、应用程序并将该应用程序添加到我的 后INSTALLED_APPS,我尝试通过AbstractUser在我的 中进行子类化来创建自定义用户类models.py

from django.contrib.auth.models import AbstractUser

class CustomUser(AbstractUser):
    pass
Run Code Online (Sandbox Code Playgroud)

然后我尝试运行makemigrations并收到此错误:

SystemCheckError: System check identified some issues:

ERRORS:
auth.User.groups: (fields.E304) Reverse accessor for 'User.groups' clashes with reverse accessor for 'CustomUser.groups'.
    HINT: Add or change a related_name argument to the definition for 'User.groups' or 'CustomUser.groups'.
auth.User.user_permissions: (fields.E304) Reverse accessor for 'User.user_permissions' clashes with reverse accessor for 'CustomUser.user_permissions'.
    HINT: Add or change a related_name argument to the definition for 'User.user_permissions' or 'CustomUser.user_permissions'.
main.CustomUser.groups: (fields.E304) …
Run Code Online (Sandbox Code Playgroud)

django django-custom-user django-migrations

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

Django:"没有安装带有标签'flatpages'的应用程序." 与django-markdown

我正在尝试为我的Django网站添加一个功能,该功能实现了由管理员写下一些降价的文章.

我安装了django-markdown,并按照本教程:http: //pythonhosted.org/django-markdown/

我makemigrations/migrate和runserver,我成功访问了我的网站,但是当我尝试访问管理界面时,我遇到了一个问题: No installed app with label 'flatpages'.

这是我的requirements.txt的内容:

Django==1.7.5
django-markdown==0.8.4
Markdown==2.6
wheel==0.24.0
Run Code Online (Sandbox Code Playgroud)

我假设它是django_markdown模块的flatpages.我尝试了一些东西,就像在INSTALLED_APPS中添加'flatpages'一样.但这并没有解决问题('django_markdown'已经在其中).我对django没有足够的经验来理解我的项目中发生了什么,我需要一些帮助才能理解这个问题.有人可以帮帮我吗?

以下是一些可能有用的附加信息和文件:

错误 :

Environment:


Request Method: GET
Request URL: http://localhost:8000/admin/

Django Version: 1.7.5
Python Version: 2.7.3
Installed Applications:
('django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django_markdown',
 'main')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware')


Traceback:
File "/home/vmonteco/Envs/django/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  111.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/vmonteco/Envs/django/local/lib/python2.7/site-packages/django/contrib/admin/sites.py" in wrapper
  227.                 return self.admin_view(view, cacheable)(*args, **kwargs)
File "/home/vmonteco/Envs/django/local/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapped_view
  105.                     response = …
Run Code Online (Sandbox Code Playgroud)

python django

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

MutationObserver错误:我怎么能等待元素的创建?

我正在尝试制作Greasemonkey脚本以在Facebook页面中添加按钮.但我需要等待创建一个元素来添加按钮.
我正在尝试使用MutationObserver(如此处所示)来执行操作,但存在错误.

function init() {
    console.log('Launched : ' + launched);
    if (!launched) {
        launched = true;
        main();
    }
}
console.log('barfoo');

var observer = new MutationObserver(function(mutations) {
    mutations.forEach(function(mutation) {
        if (!mutation.addedNodes) return;
        console.log(mutation.addedNodes.type());
        for (var i = 0; i < mutation.addedNodes.length; i++) {
            if (mutation.addedNodes[i].matches('div[class="pam _5shk uiBoxWhite bottomborder"]')) {
                console.log('init');
                init();
            }
        }
    })
});
console.log('bar');

try {
    observer.observe(document.body, {
        childlist: true,
        subtree: true,
        attributes: false,
        characterData: false,
    });
} catch (error) {
    console.log(error);
} …
Run Code Online (Sandbox Code Playgroud)

javascript firefox greasemonkey mutation-observers

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