小编Dar*_*ech的帖子

Django request.is_ajax返回false

我正在使用Django + jQuery测试ajax.我的意见中有这个:

def ajax_test(request):
    if request.is_ajax():
        message = "This is ajax"
    else:
        message = "Not ajax"
    return HttpResponse(message)
Run Code Online (Sandbox Code Playgroud)

这在我的模板中:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <script type="text/javascript" src="{{ STATIC_URL }}js/jquery-1.6.4.js"></script>
</head>
<body>
<script>
$.get("/ajax_test", function(data) {
    alert(data);
    });
});
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

我的问题是 - 为什么这会将"Not ajax"返回到我的html页面?

django ajax

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

在linux上使用apache设置子域

我不敢相信我之前没有这样做过,但我希望得到一个明确的答案,所以我已经准备好了.

我有一个apache配置文件,/etc/apache2/sites-available/mysite其中如下所示:

<VirtualHost *:80>
    ServerAdmin webmaster@localhost

    DocumentRoot /home/sam/public_html
    <Directory />
            Options FollowSymLinks
            AllowOverride All
    </Directory>
    <Directory /home/sam/public_html>
            Options Indexes FollowSymLinks MultiViews
            AllowOverride All
            Order allow,deny
            allow from all
    </Directory>

    ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
    <Directory "/usr/lib/cgi-bin">
            Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
            Order allow,deny
            Allow from all
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log

    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel warn

    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Run Code Online (Sandbox Code Playgroud)

因此,这提供了~/public_html所有罚款的HTML和PHP文件.但我有多个项目,所以想开始使用子域名.我想要做的是从~/public_html/myproject/根目录提供文件myproject.localhost.

我已经尝试将以下内容添加到我的apache文件的底部:

<VirtualHost myproject.localhost>
    DocumentRoot ~/public_html/myproject/
    ServerName …
Run Code Online (Sandbox Code Playgroud)

linux apache subdomain

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

AngularJS使用拦截器来处理$ http 404s - 承诺未定义错误

我有一个Angular应用程序,我想为404处理API端点.关键组件是这样的:

// app.js

var myApp = angular.module('myApp', ['ngRoute',]);

myApp.config( function ($httpProvider, $interpolateProvider, $routeProvider) {
  $httpProvider.interceptors.push('httpRequestInterceptor');

  $routeProvider
    ...
    .when('/project/:projectId', {
        templateUrl : 'partials/project_detail.tmpl.html',
        controller: 'ProjectDetailCtrl',
        resolve: {
            project: function ($route, ConcernService) {
                return ConcernService.get('projects/', $route.current.params.projectId);
            },
        }
    });
});


// interceptors.js

myApp.factory('httpRequestInterceptor', function ($q, $location) {
  return {
    response: function(response){
      return promise.then(
        function success(response) {
          return response;
        },
        function error(response) {
          if(response.status === 404){
            $location.path('/404');
            return $q.reject(response);
          }
          else{
            return $q.reject(response); 
          }
        }
      );
    }
  };
});


// services.js

myApp.factory('ConcernService', function …
Run Code Online (Sandbox Code Playgroud)

angularjs angular-http

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

Angular UI路由器 - 在父级中访问状态参数

我有一个使用伟大的ui路由器的Angular应用程序.

我的设置如下:

.config( function ($stateProvider, $urlRouterProvider) {

    $stateProvider

    // PROJECT DETAIL
    .state('project', {
        url: '/project/:projectId/',
        resolve: {
            /* some base api calls */
        },
        views: {
            'task-list': {
                templateUrl: 'partials/task_list.tmpl.html',
                controller: 'TaskListCtrl',
                resolve: {
                    tasks: function ($stateParams, ConcernService) {
                        return ConcernService.get('project-tasks/', $stateParams.projectId);
                    },
                }
            },
            'concern-instance': {
                /* some resolved variables */
            }
        }
    })
    .state('project.task', {
        url: 'task/:taskId/',
        views: {
            'concern-instance@': {
                /* some different resolved variables */
            },
        }
    })
Run Code Online (Sandbox Code Playgroud)

这一切都像黄油一样.然而,在我的task_list模板时,状态project.task,我希望能够访问taskIdPARAM,所以我可以强调积极的导航链接,即使网址为#/project/123/task/456/ …

angularjs angular-ui angular-routing

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

GeoDjango - GDAL库给出错误

我试图让GeoDjango在ubuntu上运行并且遇到了GDAL的问题.

我已经下载并安装了GDAL而没有任何问题.

我必须在项目设置中添加以下行:

GDAL_LIBRARY_PATH = '/usr/local/lib/libgdal.so.1.15.1'  
Run Code Online (Sandbox Code Playgroud)

当我签入shell时一切都很好:

In [1]: from django.contrib.gis import gdal
In [2]: gdal.HAS_GDAL
Out[2]: True
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试在官方教程中运行ogrinfo时,我收到以下错误:

$ ogrinfo world/data/TM_WORLD_BORDERS-0.3.shp
ogrinfo: error while loading shared libraries: libgdal.so.1: cannot open shared object file: No such file or directory
Run Code Online (Sandbox Code Playgroud)

GDAL文件位于:

/usr/local/lib/libgdal.a
/usr/local/lib/libgdal.la
/usr/local/lib/libgdal.so
/usr/local/lib/libgdal.so.1
/usr/local/lib/libgdal.so.1.15.1
Run Code Online (Sandbox Code Playgroud)

libgdal.solibgdal.so.1符号链接到libgdal.so.15.1

任何帮助将非常感激.

gdal geodjango

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

托管私人Sphinx文档

我目前正在使用Sphinx为一个相当复杂的Django网站编写一些大量文档.我一直在内部工作,在我很快离开之前,我想留下新的开发人员/管理员可以访问的详细文档.

我的问题是我有什么选择来托管这个文档?我希望它是私人的.文档也涉及网站而不是python包.出于这两个原因,我认为阅读文档可能不是最合适的选择.

我跑过django-sphinxdoc,虽然这需要设置Haystack作为后端.有没有办法简单地使用内置的js搜索服务器文档?或者允许私人托管文档的服务?

任何选项都将非常感激.

django python-sphinx

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

指定端口时出现Django runserver错误

我最近习惯于在我的django项目中执行以下操作,以便我可以在各种操作系统(即非Linux)上测试bowser兼容性:

$ sudo ./manage.py runserver 0.0.0.0:80
Run Code Online (Sandbox Code Playgroud)

这允许我通过网络上的任何机器访问项目.

但是,我只是设置了一台新机器,此命令发出以下错误:

Traceback (most recent call last):
   File "manage.py", line 8, in <module>
     from django.core.management import execute_from_command_line
ImportError: No module named django.core.management
Run Code Online (Sandbox Code Playgroud)

我知道django在查找模块时遇到了问题,我不明白的是那个简单的旧模块:

$ sudo ./manage.py runserver
Run Code Online (Sandbox Code Playgroud)

运行很好.我在这里所做的一切都在改变港口吗?当然,它在过去运作良好.

注意
1.我正在使用Django 1.4
2.我在virtualenv和系统上尝试过,我得到了相同的结果.
3.我没有安装django系统(仅限于virtualenvs)

任何帮助将非常感激.

django

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

angularJs TypeError:将循环结构转换为JSON

我有一个像我这样的小型AngularJs应用程序:

 // html

 <div ng-repeat="project in projects">
     <h3>{{ project.id }}</h3>
     <h3>{{ project.title }}</h3>
      <div ng-repeat="task in project.tasks">
          <h4>{{ task.id }}. {{ task.title }}</h4>
          <button class="btn btn-default" ng-click="showEditTask=true">Edit Task</button>
          <div class="box row animate-show-hide" ng-show="showEditTask">

              <h2>Create a New Task</h2>
              <form name="newTaskForm" class="form-horizontal">
                  Title: <br />
                  <input ng-model="new_task.title" type="text" id="title" name="title" class="form-control" placeholder="Title" required /><br />

                  Project: <br />
                  <select ng-model="new_task.project" ng-options="project.title for project in projects" class="form-control"></select><br>
              </form>

                  <button class="btn btn-success" ng-click="createTask(new_task)" ng-disabled="!newTaskForm.title.$valid">create</button>
          </div>
      </div>
 </div>


  // app.js

  concernsApp.factory('ConcernService', function ($http, $q) { …
Run Code Online (Sandbox Code Playgroud)

json angularjs angularjs-scope

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

Django在测试中测试存储的会话数据

我有这样的观点:

def ProjectInfo(request):
    if request.method == 'POST':
        form = ProjectInfoForm(request.POST)
        if form.is_valid():
            # if form is valid, iterate cleaned form data
            # and save data to session
            for k, v in form.cleaned_data.iteritems():
                request.session[k] = v
            return HttpResponseRedirect('/next/')
        else:
            ...
    else:
        ...
Run Code Online (Sandbox Code Playgroud)

在我的测试中:

from django.test import TestCase, Client
from django.core.urlresolvers import reverse
from tool.models import Module, Model
from django.contrib.sessions.models import Session

def test_project_info_form_post_submission(self):
    """
    Test if project info form can be submitted via post.
    """
    # set up our POST data …
Run Code Online (Sandbox Code Playgroud)

django django-testing

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

Android上的Python + Django

我是一名Django开发人员,想知道是否有人知道使用Android平板电脑(如nexus 7)在Django上安装和开发的可能性.这看起来像一个功能相当强大的设备,可以与蓝牙键盘连接,并将Linux作为操作系统的核心.

那么 - 是否可以在Android上安装Python和Django(甚至是Flask)?

python django android

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