小编apa*_*des的帖子

Python访问嵌套的JSON数据

我正在尝试使用zippopotam.us获取特定城市的邮政编码.我有以下代码可用,除非我尝试访问post code返回的键TypeError: expected string or buffer

r = requests.get('http://api.zippopotam.us/us/ma/belmont')
j = r.json()

data = json.loads(j)

print j['state']
print data['places']['latitude']
Run Code Online (Sandbox Code Playgroud)

完整的JSON输出:

{
"country abbreviation": "US",
"places": [
    {
        "place name": "Belmont",
        "longitude": "-71.4594",
        "post code": "02178",
        "latitude": "42.4464"
    },
    {
        "place name": "Belmont",
        "longitude": "-71.2044",
        "post code": "02478",
        "latitude": "42.4128"
    }
],
"country": "United States",
"place name": "Belmont",
"state": "Massachusetts",
"state abbreviation": "MA"
}
Run Code Online (Sandbox Code Playgroud)

谢谢你的帮助.

python rest json

45
推荐指数
4
解决办法
14万
查看次数

Flask request.args vs request.form

我的理解是,request.args在Flask中包含来自GET请求的URL编码参数,同时request.form包含POST数据.我很难掌握的是为什么在发送POST请求时,尝试访问数据时request.form返回400错误但是当我尝试使用它时request.args它似乎工作正常.

我试图发送具有两个请求Postmancurl,结果是相同的.

curl -X POST -d {"name":"Joe"} http://127.0.0.1:8080/testpoint --header "Content-Type:application/json"
Run Code Online (Sandbox Code Playgroud)

码:

@app.route('/testpoint', methods = ['POST'])
def testpoint():
    name = request.args.get('name', '')
    return jsonify(name = name)
Run Code Online (Sandbox Code Playgroud)

python rest post curl flask

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

Gem安装调试器错误

我在Mac OS 10.9.2上运行Rails v4.0.2并且我顺利完成了一切.我突然发现一个错误,当我尝试时,宝石丢失了rails server.我运行bundle install,这是输出:

Fetching gem metadata from https://rubygems.org/.......
Fetching additional metadata from https://rubygems.org/..
Using rake 10.1.1
Using i18n 0.6.9
Using minitest 4.7.5
Using multi_json 1.9.0
Using atomic 1.1.16
Using thread_safe 0.2.0
Using tzinfo 0.3.39
Using activesupport 4.0.2
Using builder 3.1.4
Using erubis 2.7.0
Using rack 1.5.2
Using rack-test 0.6.2
Using actionpack 4.0.2
Using mime-types 1.25.1
Using polyglot 0.3.4
Using treetop 1.4.15
Using mail 2.5.4
Using actionmailer 4.0.2
Using activemodel 4.0.2
Using activerecord-deprecated_finders 1.0.3
Using arel …
Run Code Online (Sandbox Code Playgroud)

ruby gem ruby-on-rails rvm

25
推荐指数
4
解决办法
3万
查看次数

Python多处理池在ubuntu服务器上挂起

我正在使用nginx和gunicorn在Ubuntu服务器上运行Django.我正在尝试进行一些在我的本地机器上工作的多处理,但是直到gunicorn工作人员在我的服务器上超时.

cpu_count = int(multiprocessing.cpu_count())
pool = Pool(processes = cpu_count)
result = pool.map_async(apiSimulAvail, rate_ranges)
result.wait()

...do some more stuff once all processes return
Run Code Online (Sandbox Code Playgroud)

它挂在pool = Pool(processes = cpu_count).我没有得到任何错误,枪炮工人只是超时并重新启动.

关于为什么会发生这种情况和/或我如何解决它的任何迹象都非常感谢.谢谢.

python django ubuntu nginx gunicorn

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

Django查询集过滤器GT,LT,GTE,LTE返回完整对象列表

我正在尝试使用数据库中的对象进行过滤,.filter(field__lte = parameter)但它只返回所有对象并且不会过滤掉任何对象.我甚至将参数设置为远高于存储在数据库中的任何值,并且仍返回所有对象.

>> all_objects = Ranked.objects.all()
>> filtered = all_objects.filter(score__lte = 100) #The max possible score is 100
>> len(filtered)
87 #Every object in the db
Run Code Online (Sandbox Code Playgroud)

我要查询的数据库中的字段是IntegerField.

我在这里做错了吗?谢谢你的帮助.

python django django-queryset

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

使用Heroku进行Django部署

我正在尝试使用heroku部署我现有的django项目,跟随heroku提供的步骤并根据需要调整以特定于我的项目.只是为了快速完成我迄今所做的工作:

  • 在我的virtualenv中安装了django-toolbelt
  • 在我的项目的根目录中创建了一个Procfile,Procile其中包括:

    web: gunicorn projectname.wsgi
    
    Run Code Online (Sandbox Code Playgroud)
  • 使用foreman start确认我的项目仍在本地正常运行

  • 创建requirements.txt使用pip freeze,并把它放在我的项目的根
  • 将以下内容添加到settings.py:

    import dj_database_url
    DATABASES['default'] =  dj_database_url.config()
    
    # Honor the 'X-Forwarded-Proto' header for request.is_secure()
    SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
    
    # Allow all host headers
    ALLOWED_HOSTS = ['*']
    
    # Static asset configuration
    import os
    BASE_DIR = os.path.dirname(os.path.abspath(__file__))
    STATIC_ROOT = 'staticfiles'
    STATIC_URL = '/static/'
    
     STATICFILES_DIRS = (
     os.path.join(BASE_DIR, 'static'),
    )
    
    Run Code Online (Sandbox Code Playgroud)

这是我有点困惑的一点,演练没有提到我现有的数据库设置会发生什么,保持原样,删除,更改?

  • 在wsgi.py中添加了以下内容:

    from django.core.wsgi import get_wsgi_application
    from dj_static import Cling
    
    application = Cling(get_wsgi_application())
    
    Run Code Online (Sandbox Code Playgroud)
  • 添加了heroku作为git遥控器并将我的项目推送到heroku.有效. …

django heroku

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

Django访问formset数据

我无法访问通过我的formset提交的数据.这是我的代码:

模板:

<form action="" method="post">
    {% csrf_token %}
    {{ formset.management_form }}
    {% for form in formset %}

    {{ form.as_p }}

    {% endfor %}

    <input type="submit" value="Submit">

</form>
Run Code Online (Sandbox Code Playgroud)

视图:

def addMembers(request, id, members):
    if request.user.is_authenticated():
        members = int(members)
        MemberFormSet = formset_factory(MemberForm, extra = members)
        if request.method == 'POST':
            print 'post'
            formset = MemberFormSet(request.POST)
            if formset.is_valid():
                cd = formset.cleaned_data
                for f in formset:
                    first_name = cd.first_name
                    last_name = cd.last_name
                    email = cd.email
                    house = House.objects.get(id = id)
                    member = Member(first_name = first_name, …
Run Code Online (Sandbox Code Playgroud)

forms django formset

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

Django Allauth自定义登录不显示错误

我在我的项目中使用Django Allauth用户流.我已成功创建了自己的模板集,这些模板继承自标准的Allauth模板,它们的工作率为95%.但是,出于某种原因,我没有收到无效登录的错误.

如果我为用户输入"test @ email",我会收到"请输入有效的电子邮件地址"错误,但如果我输入的密码不正确的电子邮件,则只需重新加载页面,但不会出现错误.我已经在下面包含了带有{{ form.login.errors }}{{ form.password.errors }}标签的模板.

感谢所有指导,谢谢.

模板:

<div id="search_container">

            <div id="search_box_content">
                {% block page_content %}

                        <h4>Login</h4>

                            <form class="" id="user-form" method="POST" action="{% url 'account_login' %}">

                              {% csrf_token %}
                            <h1> {{ form.login.errors }}</h1>
                            <h1> {{ form.password.errors }}</h1>

                               <div class="input_class">

                                    <label for="login">Username: </label>
                            {{ form.login }}
                            </div>

                            <div class="input_class">
                              <label for="password">Password: </label>
                              {{ form.password }}
                            </div>
                            <div class="input_class" id="forgot_pass">
                              <label for="remember">
                              Remember Me? </label>
                            {{ form.remember }}
                            </div>
                              {% if redirect_field_value %}


                              <input type="hidden" name="{{ …
Run Code Online (Sandbox Code Playgroud)

html python django django-allauth

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

增加Memcached最大项目大小

我正在尝试增加Memcached安装中的最大项目大小。

到目前为止,我尝试过编辑/etc/memcached.conf添加行,-I 3M然后重新启动memcached。

这对我有用,但似乎在一夜之间恢复了。该设置仍然存在,但是当项目超过1MB时,将清除缓存。

您可以在此处看到我的原始问题Django Memcached缓存消失

我也尝试输入memcached -I 3m返回以下命令:

WARNING: Setting item max size above 1MB is not recommended!
Raising this limit increases the minimum memory requirements and will decrease your memory efficiency.
can't run as root without the -u switch 
Run Code Online (Sandbox Code Playgroud)

随后运行memcached -Iu 3m返回:

Item max size cannot be less than 1024 bytes.
Run Code Online (Sandbox Code Playgroud)

我不知道这是什么意思,因为我试图增加最大大小而不是减小它,并且3MB肯定大于1024字节。我真的很受困,感谢所有帮助。谢谢。

更新:

在找到有关memcached的MySQL文档之后,我了解到-u标志的工作原理如下memcached -u root -I 3m。这会运行(仍然带有上面的警告),但随后会挂起。我一直盯着航站楼约10分钟,希望它能返回我的提示,但没有运气。请帮忙。

ubuntu memcached caching python-memcached

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

Django没有与参数反向匹配

从我的模板:

<a href="{% url 'tracker:othermonth' year next_month %}">July</a>
Run Code Online (Sandbox Code Playgroud)

网址格式:

url(r'^ocal/$', views.calendar, name = "othermonth"),
Run Code Online (Sandbox Code Playgroud)

视图:

def calendar(request, year, month):
    my_year = int(year)
    my_month = int(month)
    my_calendar_from_month = datetime(my_year, my_month, 1)
    my_calendar_to_month = datetime(my_year, my_month, monthrange(my_year, my_month)[1])

    my_tickets = Event.objects.filter(on_sale__gte=my_calendar_from_month).filter(on_sale__lte=my_calendar_to_month)

    my_previous_year = my_year
    my_previous_month = my_month - 1
    if my_previous_month == 0:
        my_previous_year = my_year - 1
        my_previous_month = 12
    my_next_year = my_year
    my_next_month = my_month + 1
    if my_next_month == 13:
        my_next_year = my_year + 1
        my_next_month = 1
    my_year_after_this …
Run Code Online (Sandbox Code Playgroud)

django url reverse arguments

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