小编Ala*_*air的帖子

Django错误:render_to_response()得到了一个意外的关键字参数'context_instance'

升级到Django 1.10后,我收到错误render_to_response() got an unexpected keyword argument 'context_instance'.

我的观点如下:

from django.shortcuts import render_to_response
from django.template import RequestContext

def my_view(request):
    context = {'foo': 'bar'}
    return render_to_response('my_template.html', context, context_instance=RequestContext(request))
Run Code Online (Sandbox Code Playgroud)

这是完整的追溯:

Traceback:

File "/Users/alasdair/.virtualenvs/django110/lib/python2.7/site-packages/django/core/handlers/exception.py" in inner
  39.             response = get_response(request)

File "/Users/alasdair/.virtualenvs/django110/lib/python2.7/site-packages/django/core/handlers/base.py" in _get_response
  187.                 response = self.process_exception_by_middleware(e, request)

File "/Users/alasdair/.virtualenvs/django110/lib/python2.7/site-packages/django/core/handlers/base.py" in _get_response
  185.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/Users/alasdair/dev/rtr/rtr/urls.py" in my_view
  26.     return render_to_response('my_template.html', context,  context_instance=RequestContext(request))

 Exception Type: TypeError at /
Exception Value: render_to_response() got an unexpected keyword argument …
Run Code Online (Sandbox Code Playgroud)

python django

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

Django错误消息"向定义添加related_name参数"

D:\zjm_code\basic_project>python manage.py syncdb
Error: One or more models did not validate:
topics.topic: Accessor for field 'content_type' clashes with related field 'Cont
entType.topic_set'. Add a related_name argument to the definition for 'content_t
ype'.
topics.topic: Accessor for field 'creator' clashes with related field 'User.crea
ted_topics'. Add a related_name argument to the definition for 'creator'.
topics.topic: Reverse query name for field 'creator' clashes with related field
'User.created_topics'. Add a related_name argument to the definition for 'creato
r'.
topicsMap.topic: Accessor for field 'content_type' clashes …
Run Code Online (Sandbox Code Playgroud)

django

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

在Django QuerySet中,如何在多对一关系中过滤"不存在"

我有两个这样的模型:

class User(models.Model):
    email = models.EmailField()

class Report(models.Model):
    user = models.ForeignKey(User)
Run Code Online (Sandbox Code Playgroud)

实际上,每个模型都有更多的字段,这些字段与此问题无关.

我想过滤所有拥有以"a"开头并且没有报告的电子邮件的用户.将有更多.filter().exclude()基于其他领域的标准.

我想这样做:

users = User.objects.filter(email__like = 'a%')

users = users.filter(<other filters>)

users = ???
Run Code Online (Sandbox Code Playgroud)

我想要 ???过滤掉没有与之关联的报告的用户.我该怎么做?如果这不可能像我提出的那样,那么另一种方法是什么?

django django-models django-orm

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

试图在Django中从POST解析`request.body`

出于某种原因,我无法弄清楚为什么Django没有request.body正确处理我的内容.

它以JSON格式发送,查看NetworkDev Tools 中的选项卡将其显示为请求有效负载:

{creator: "creatorname", content: "postcontent", date: "04/21/2015"}
Run Code Online (Sandbox Code Playgroud)

这正是我希望它发送到我的API的方式.

在Django中,我有一个视图接受此请求作为参数,仅用于我的测试目的,应打印request.body["content"]到控制台.

当然,没有任何东西被打印出来,但是当我打印时,request.body我得到了这个:

b'{"creator":"creatorname","content":"postcontent","date":"04/21/2015"}'
Run Code Online (Sandbox Code Playgroud)

所以我知道我确实有一具尸体.

我尝试过json = json.loads(request.body)也无济于事.json设置该变量后打印也不返回任何内容.

python django json python-3.x backbone.js

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

将整数转换为字符串以在运行时创建输出文件名

我在Fortran中有一个程序将结果保存到文件中.目前我使用打开文件

OPEN (1, FILE = 'Output.TXT')
Run Code Online (Sandbox Code Playgroud)

不过,我现在想运行一个循环,每次迭代的结果保存到文件'Output1.TXT','Output2.TXT','Output3.TXT',等等.

在Fortran中有一种简单的方法可以从循环计数器构造文件名i吗?

string fortran integer

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

python'不是'运营商

我注意到有一个比较运算符is not.我应该把它翻译成

!= 
Run Code Online (Sandbox Code Playgroud)

代替

== not
Run Code Online (Sandbox Code Playgroud)

python

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

Django ImportError:无法从“django.shortcuts”导入名称“render_to_response”

升级到 Django 3.0 后,出现以下错误:

ImportError: cannot import name 'render_to_response' from 'django.shortcuts'
Run Code Online (Sandbox Code Playgroud)

我的看法:

from django.shortcuts import render_to_response

from django.template import RequestContext

def index(request):
    context = {'foo': 'bar'}
    return render_to_response('index.html', context, context_instance=RequestContext(request))
Run Code Online (Sandbox Code Playgroud)

这是完整的回溯:

Traceback (most recent call last):
  File "./manage.py", line 21, in <module>
    main()
  File "./manage.py", line 17, in main
    execute_from_command_line(sys.argv)
  File "/Users/alasdair/.virtualenvs/django30/lib/python3.7/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "/Users/alasdair/.virtualenvs/django30/lib/python3.7/site-packages/django/core/management/__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/alasdair/.virtualenvs/django30/lib/python3.7/site-packages/django/core/management/base.py", line 323, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/Users/alasdair/.virtualenvs/django30/lib/python3.7/site-packages/django/core/management/commands/runserver.py", line 60, in execute
    super().execute(*args, **options) …
Run Code Online (Sandbox Code Playgroud)

python django django-views python-3.x django-3.0

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

使用内联表单集创建模型和相关模型

[我已经在Django用户发布了这个| Google网上论坛也是.]

使用内联formset文档中的示例,我能够编辑属于特定模型的对象(使用模型形式).我一直在尝试使用相同的模式来 创建使用内联表单集的新对象,但是无法清除我的脑袋以便为此目的带出一个工作视图.

使用与上述链接相同的示例,我将如何创建"作者"模型的新实例及其相关的"书籍"对象?

django modelform inline-formset

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

Django 2.0路径错误?:(2_0.W001)的路由包含'(?P <',以'^'开头,或以'$'结尾

我是Django的新手,正在尝试在我的网站上为音乐应用程序创建后端代码.

我在views.py文件中创建了正确的视图(在正确的目录中),如下所示:

def detail(request, album_id):
    return HttpResponse("<h1>Details for Album ID:" + str(album_id) + "</h1>")
Run Code Online (Sandbox Code Playgroud)

但是,为此创建URL或路径时(如下所示)

#/music/71/ (pk)
path(r'^(?P<album_id>[0-9])/$', views.detail, name='detail'),
Run Code Online (Sandbox Code Playgroud)

我在终端上遇到警告:

?: (2_0.W001) Your URL pattern '^(?P<album_id>[0-9])/$' [name='detail'] has a route that contains '(?P<', begins with a '^', or ends with a '$'. This was likely an oversight when migrating to django.urls.path().
Run Code Online (Sandbox Code Playgroud)

每当/music/(路径工作的)后面跟一个数字,例如/music/1(这是我想做的),就找不到页面,终端给出了上述警告.

这可能是一个简单的错误,只是我是愚蠢但我是Django和python正则表达式语句的新手,所以任何帮助表示赞赏.

python django django-urls django-views python-3.x

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

Django弃用警告或错误配置错误 - 不支持将3元组传递给django.conf.urls.include()

我在Django 1.11中有一个弃用警告:

RemovedInDjango20Warning: Passing a 3-tuple to django.conf.urls.include() is deprecated. Pass a 2-tuple containing the list of patterns and app_name, and provide the namespace argument to include() instead.
  url(r'^admin/', include(admin.site.urls))
Run Code Online (Sandbox Code Playgroud)

在Django 2.0中,这给出了错误:

django.core.exceptions.ImproperlyConfigured: Passing a 3-tuple to include() is not supported. 
Pass a 2-tuple containing the list of patterns and app_name, and provide the namespace argument to include() instead.
Run Code Online (Sandbox Code Playgroud)

我该怎么改变url(r'^admin/', include(admin.site.urls))?我试着查看文档,但我不知道......

这是我的urls.py:

from django.conf.urls import include, url
from django.contrib import admin
admin.autodiscover()

urlpatterns = [
    url(r'^admin/', include(admin.site.urls)),
    url(r'^admin/django-ses/', …
Run Code Online (Sandbox Code Playgroud)

python django

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