试图抓住Django的基础知识.即应用程序如何工作.文档:https://docs.djangoproject.com/en/stable/ref/applications/#methods
在AppConfig类的代码中我们可以读到:
def ready(self):
"""
Override this method in subclasses to run code when Django starts.
"""
Run Code Online (Sandbox Code Playgroud)
嗯,这是我的例子:
my_app应用/ apps.py
class MyAppConfig(AppConfig):
name = 'my_app'
def ready(self):
print('My app')
Run Code Online (Sandbox Code Playgroud)
我只想让ready方法工作.也就是说,当Django找到my_app时,让它运行ready方法.
该应用程序已在INSTALLED_APPS中注册.
我执行'python manage.py runserver'.什么都没打印出来.
如果我在ready方法中放置一个断点,那么调试器就不会停在那里.
你能帮助我吗:我在这里理解的错误是什么?先感谢您.
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'my_app',
]
Run Code Online (Sandbox Code Playgroud)
我创建了一个视图
my_app应用/ views.py
def index(request):
print('Print index')
Run Code Online (Sandbox Code Playgroud)
urls.py
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$', my_app_views.index, name='home')
]
Run Code Online (Sandbox Code Playgroud)
嗯,这个观点很有效.这意味着该应用程序已注册.
Django 1.11.4
我已经用method =“get”建立了一个搜索表单。搜索表单有很多表单。然后这个输入值作为获取参数在 url 中传输。
问题是如何获得分页。该数据库包含数千个对象。分页是必要的。
这是文档告诉我们的:
https://docs.djangoproject.com/en/1.11/topics/pagination/#using-paginator-in-a-view
它建议如下:
<a href="?page={{ contacts.previous_page_number }}">previous</a>
Run Code Online (Sandbox Code Playgroud)
但这会破坏所有获取参数。
我设法发明的是:
<a href="{{ request.get_full_path }}&page={{ object_list.previous_page_number }}">previous</a>
Run Code Online (Sandbox Code Playgroud)
这有效。但这是愚蠢的。如果向前和向后切换页面,它会产生如下结尾的网址:
page=2&page=3&page=2
Run Code Online (Sandbox Code Playgroud)
我看过谷歌是如何解决这个问题的。在 url 的中间,他们有 start=30。并更改此参数:start=20, start=40。所以,他们换了。
你能帮我理解如何在 Django 中保存获取参数和切换页面吗?当然,以一种优雅的方式。
我正在读一本关于码头的书.它已经有几年了.
我会引用:
If you want to get rid of all your stopped containers, you can use
the output of docker ps -aq -f status=exited , which gets the
IDs of all stopped containers. For example:
$ docker rm -v $(docker ps -aq -f status=exited)
Run Code Online (Sandbox Code Playgroud)
当我运行这个时,我得到:
michael@michael-desktop:~$ sudo docker rm -v $(docker ps -aq -f status=exited)
Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get http://%2Fvar%2Frun%2Fdocker.sock/v1.30/containers/json?all=1&filters=%7B%22status%22%3A%7B%22exited%22%3Atrue%7D%7D: dial unix /var/run/docker.sock: connect: permission denied
"docker rm" requires …Run Code Online (Sandbox Code Playgroud) class Controller:
def __init__(self):
self.__whiteList = self.readFile('whiteList.txt')
a = 0 # Breakpoint
def getWhiteList(self):
return self.__whiteList
Run Code Online (Sandbox Code Playgroud)
好吧,我给了一个断点a = 0.
当我在断点处停下来时,我想评估一下__whiteList.
错误是:
AttributeError:'Controller' object has no attribute '__whiteList'
Run Code Online (Sandbox Code Playgroud)
嗯,这对我来说是一个谜.因为我有getter方法并且在课外,它完美地工作.
好吧,你可以告诉我,我可以很容易地不注意它,因为它在课外工作.但是在调试过程中我需要它.
你能评论为什么我不能在断点处抓住价值吗?
我使用PyCharm Community Edition 3.4.
我已添加self.__a到手表.
这是我的例子:
class Box:
def __init__(self, a, b, c):
self.__a = a
self._b = b
self.c = c
d = 0 #Breakpoint.
a = Box(1, 2, 3)
Run Code Online (Sandbox Code Playgroud)
所以,我开始调试并在断点处停止.该self.__a表显示{AttributeError}'Box' object has no attribute 'a'.
我按Alt+ F8并评估self.__a = a.结果是None.
然后我评估self.__a,结果是1.
我的手表self.__a仍然显示{AttributeError}'Box' object has no attribute 'a'.我删除它.然后我又添了一块手表self.__a.它表明1.
你能澄清一下这里发生了什么吗?
Ubuntu 16.04.3
我想安装pgAdmin:
我用python 2创建了一个virtualenv.
然后安装pgAdmin 4 v2.0:
pip install https://ftp.postgresql.org/pub/pgadmin/pgadmin4/v2.0/pip/pgadmin4-2.0-py2.py3-none-any.whl
Run Code Online (Sandbox Code Playgroud)
是时候运行pgAdmin了:
(pgadmin4) michael@michael-desktop:~/PycharmProjects/venv$ python pgadmin4/lib/python2.7/site-packages/pgadmin4/pgAdmin4.py
Traceback (most recent call last):
File "pgadmin4/lib/python2.7/site-packages/pgadmin4/pgAdmin4.py", line 55, in <module>
exec(open(file_quote(setupfile), 'r').read())
File "<string>", line 46, in <module>
File "/home/michael/PycharmProjects/venv/pgadmin4/lib/python2.7/site-packages/pgadmin4/pgadmin/setup/data_directory.py", line 23, in create_app_data_directory
_create_directory_if_not_exists(os.path.dirname(config.SQLITE_PATH))
File "/home/michael/PycharmProjects/venv/pgadmin4/lib/python2.7/site-packages/pgadmin4/pgadmin/setup/data_directory.py", line 15, in _create_directory_if_not_exists
os.mkdir(_path)
OSError: [Errno 13] Permission denied: '/var/lib/pgadmin'
Run Code Online (Sandbox Code Playgroud)
你能帮我一把吗?
使用Ubuntu 16.04.2主机。
Docker版本 17.06.0-ce。
文件
RUN echo -e "deb http://nginx.org/packages/mainline/ubuntu/ xenial nginx\ndeb-src http://nginx.org/packages/mainline/ubuntu/ xenial nginx" | tee /etc/apt/sources.list.d/nginx.list
Run Code Online (Sandbox Code Playgroud)
这导致/etc/apt/sources.list.d/nginx.list:
-e deb http://nginx.org/packages/mainline/ubuntu/ xenial nginx
deb-src http://nginx.org/packages/mainline/ubuntu/ xenial nginx
Run Code Online (Sandbox Code Playgroud)
好吧,好像对反斜杠转义的解释有效。但是这个“-e”已经出现在文件中。
然后无法构建这样的图像。
你能在这里踢我一脚吗?
Ubuntu 18.04
我正在使用 Sublime Text 3.1.1 设置 Xdebug。我正在尝试为 PHP 7.2 组织断点。
我删除了日志文件。它存在。我看了看,决定它需要清洁。然后我就做了
sudo rm xdebug.log
Run Code Online (Sandbox Code Playgroud)
然后我在 Sublime Text 中运行:工具/Xdebug/开始调试(启动浏览器)。
嗯,文件没有出现。然后我开始调试几次,至少 Xdebug 在日志中写了一些东西。
你能告诉我每次开始调试时 Xdebug 是否会向日志文件写入一些内容吗?如果不是,写入日志的条件是什么?
配置文件
[xdebug]
zend_extension="/usr/lib/php/20170718/xdebug.so"
xdebug.remote_enable=1
xdebug.idekey="sublime.xdebug"
xdebug.remote_autostart=1
xdebug.remote_log = "/var/log/xdebug.log"
Run Code Online (Sandbox Code Playgroud)
phpinfo()
xdebug
xdebug support => enabled
Version => 2.6.1
IDE Key => sublime.xdebug
Supported protocols
DBGp - Common DeBuGger Protocol
Directive => Local Value => Master Value
xdebug.auto_trace => Off => Off
xdebug.cli_color => 0 => 0
xdebug.collect_assignments => Off => Off
xdebug.collect_includes => On => …Run Code Online (Sandbox Code Playgroud) Django 1.9.6
我想写一些单元测试来检查重定向.
你能帮我理解我在这里做错了什么.
先感谢您.
考试:
from django.test import TestCase
from django.core.urlresolvers import reverse
from django.http.request import HttpRequest
from django.contrib.auth.models import User
class GeneralTest(TestCase):
def test_anonymous_user_redirected_to_login_page(self):
user = User(username='anonymous', email='vvv@mail.ru', password='ttrrttrr')
user.is_active = False
request = HttpRequest()
request.user = user
hpv = HomePageView()
response = hpv.get(request)
self.assertRedirects(response, reverse("auth_login"))
Run Code Online (Sandbox Code Playgroud)
结果:
回溯(最近一次调用最后一次):文件"/home/michael/workspace/photoarchive/photoarchive/general/tests.py",第44行,在test_anonymous_user_redirected_to_login_page中self.assertRedirects(响应,反向("auth_login"))文件"/ home /michael/workspace/venvs/photoarchive/lib/python3.5/site-packages/django/test/testcases.py",第326行,在assertRedirects中redirect_response = response.client.get(path,QueryDict(query),AttributeError: 'HttpResponseRedirect'对象没有属性'client'
在0.953s中进行3次测试
什么pdb说:
-> self.assertRedirects(response, reverse("auth_login"))
(Pdb) response
<HttpResponseRedirect status_code=302, "text/html; charset=utf-8", url="/accounts/login/">
Run Code Online (Sandbox Code Playgroud) Ubuntu 16.04
Python 3.5.2
Run Code Online (Sandbox Code Playgroud)
如果我运行python,则在virtualenv内部,将得到Python 3.5.2。
问题是:
(photoarchive) admin@simple_project:~/venv/photoarchive/lib/python3.5/encodings$ pip install django-crequest
Collecting django-crequest
Using cached django-crequest-2016.3.16.tar.gz
Complete output from command python setup.py egg_info:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/tmp/pip-build-2qlcw5ux/django-crequest/setup.py", line 9, in <module>
license=open('LICENSE').read(),
File "/home/admin/venv/photoarchive/lib/python3.5/encodings/ascii.py", line 26, in decode
return codecs.ascii_decode(input, self.errors)[0]
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in position 204: ordinal not in range(128)
----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-2qlcw5ux/django-crequest/
Run Code Online (Sandbox Code Playgroud)
好吧,点安装会破坏一切。顺便说一下,在没有virtualenv的情况下,该软件包是通过pip安装的。
我被困住了,不知道该如何应对。
我唯一想到的就是在ascii.py中的第26行停止。这似乎可以通过pdb实现。我将对其进行编辑,然后在其中添加pdb.set_trace()。然后保存文件。 …
django ×2
docker ×2
python ×2
attributes ×1
class ×1
django-tests ×1
dockerfile ×1
pdb ×1
pgadmin-4 ×1
pycharm ×1
xdebug ×1