我希望网站上的用户能够下载路径被遮挡的文件,因此无法直接下载.
例如,我希望URL是这样的," http://example.com/download/?f=somefile.txt
在服务器上,我知道所有可下载文件都位于"/ home/user/files /"文件夹中.
有没有办法让Django提供该文件供下载,而不是试图找到一个URL和View来显示它?
有没有一个很好的方法在django中执行此操作而不必滚动我自己的身份验证系统?我希望用户名是用户的电子邮件地址,而不是他们创建用户名.
请指教,谢谢.
我有一个具有日期时间字段的模型:
date = models.DateField(_("Date"), default=datetime.now())
当我在内置的django admin中检查应用程序时,DateField也会附加时间,因此如果您尝试保存它,则会返回错误.如何使默认日期成为日期?(datetime.today()也不起作用)
我的服务器直到昨天都做得很好.它正在运行Redmine,它是最快乐的小服务器,直到我的"朋友"导入了我的小家伙无法接受的SQL表.不幸的是,经过一个小时的尝试让小伙伴做出回应,我们不得不重新启动他.
现在重启后,我们在尝试访问连接到Redmine的域时遇到503错误.它连接到Mongrel守护程序,我们使用Apache Proxy将所有连接定向到Redmine正在运行的端口.
在服务器(http://localhost:8000
)上使用Lynx,您可以看到Ruby应用程序正常运行.但是这个位在我的Apache配置文件中不起作用:
<VirtualHost *:80>
ServerName sub.example.com
ProxyPass / http://localhost:8000
ProxyPassReverse / http://localhost:8000
ProxyPreserveHost on
LogLevel debug
</VirtualHost>
Run Code Online (Sandbox Code Playgroud)
这是Apache的错误日志输出:
[debug] mod_proxy_http.c(54): proxy: HTTP: canonicalising URL //localhost:8000 [debug] proxy_util.c(1335): [client 216.27.137.51] proxy: http: found worker http://localhost:8000 for http://localhost:8000/ [debug] mod_proxy.c(756): Running scheme http handler (attempt 0) [debug] mod_proxy_http.c(1687): proxy: HTTP: serving URL http://localhost:8000/ [debug] proxy_util.c(1755): proxy: HTTP: has acquired connection for (localhost) [debug] proxy_util.c(1815): proxy: connecting http://localhost:8000/ to localhost:8000 [debug] proxy_util.c(1908): proxy: …
最好的方法是什么?我不是命令行战士,但我认为有可能使用grep和cat.
我只想替换文件夹和子文件夹中出现的字符串.最好的方法是什么?如果重要的话,我正在运行ubuntu.
-d
我正在尝试用Python的lxml库解析一个超过2GB的XML文件.不幸的是,XML文件没有告诉字符编码的行,所以我必须手动设置它.虽然迭代文件,但仍有一些奇怪的字符偶尔会出现.
我不确定如何确定行的字符编码,但此外,lxml将从for循环的范围引发XMLSyntaxError.如何正确捕获此错误并正确处理?这是一个简单的代码片段:
from lxml import etree
etparse = etree.iterparse(file("my_file.xml", 'r'), events=("start",), encoding="CP1252")
for event, elem in etparse:
if elem.tag == "product":
print "Found the product!"
elem.clear()
Run Code Online (Sandbox Code Playgroud)
这最终会产生错误:
XMLSyntaxError: PCDATA invalid Char value 31, line 1565367, column 50
该文件的那一行看起来像这样:
% sed -n "1565367 p" my_file.xml
<romance_copy>Ravioli Florentine. Tender Ravioli Filled With Creamy Ricotta Cheese And
Run Code Online (Sandbox Code Playgroud)
填充的'F'实际上在我的终端中看起来像这样:
当我在虚拟环境中时,我尝试运行:
pip install MySQL-python
Run Code Online (Sandbox Code Playgroud)
这不起作用,所以我尝试下载包并通过运行安装它:
python setup.py install
Run Code Online (Sandbox Code Playgroud)
这将返回以下错误:
% python setup.py install ~VIRTUAL_ENV/build/MySQL-python running install install_dir /home/damon/Workspace/django-projects/acm-cie/env/lib/python2.6/site-packages/ running bdist_egg running egg_info writing MySQL_python.egg-info/PKG-INFO writing top-level names to MySQL_python.egg-info/top_level.txt writing dependency_links to MySQL_python.egg-info/dependency_links.txt reading manifest file 'MySQL_python.egg-info/SOURCES.txt' reading manifest template 'MANIFEST.in' warning: no files found matching 'MANIFEST' warning: no files found matching 'ChangeLog' warning: no files found matching 'GPL' writing manifest file 'MySQL_python.egg-info/SOURCES.txt' installing library code to build/bdist.linux-x86_64/egg running install_lib running build_py copying MySQLdb/release.py -> build/lib.linux-x86_64-2.6/MySQLdb running build_ext building '_mysql' …
django.contrib.postgres的新TrigramSimilarity功能非常适合我遇到的问题.我将它用于搜索栏以找到难以拼写的拉丁名字.问题是有超过200万个名字,搜索需要的时间比我想要的要长.
我想在postgres文档https://www.postgresql.org/docs/9.6/static/pgtrgm.html中创建一个关于三元组的索引.
但我不知道如何以Django API使用它的方式来做到这一点.对于postgres文本搜索,有关于如何创建索引的描述.但不是因为三元组的相似性. https://docs.djangoproject.com/en/1.11/ref/contrib/postgres/search/#performance
这就是我现在所拥有的:
class NCBI_names(models.Model):
tax_id = models.ForeignKey(NCBI_nodes, on_delete=models.CASCADE, default = 0)
name_txt = models.CharField(max_length=255, default = '')
name_class = models.CharField(max_length=32, db_index=True, default = '')
class Meta:
indexes = [GinIndex(fields=['name_txt'])]
Run Code Online (Sandbox Code Playgroud)
然后在vieuw的get_queryset中我做:
class TaxonSearchListView(ListView):
#form_class=TaxonSearchForm
template_name='collectie/taxon_list.html'
paginate_by=20
model=NCBI_names
context_object_name = 'taxon_list'
def dispatch(self, request, *args, **kwargs):
query = request.GET.get('q')
if query:
try:
tax_id = self.model.objects.get(name_txt__iexact=query).tax_id.tax_id
return redirect('collectie:taxon_detail', tax_id)
except (self.model.DoesNotExist, self.model.MultipleObjectsReturned) as e:
return super(TaxonSearchListView, self).dispatch(request, *args, **kwargs)
else:
return super(TaxonSearchListView, self).dispatch(request, *args, **kwargs)
def get_queryset(self): …
Run Code Online (Sandbox Code Playgroud) 我想grep我的服务器日志来收集所有唯一的IP地址,并且还要查看特定页面的所有请求.做这个的最好方式是什么?
python ×7
django ×4
apache ×1
download ×1
encoding ×1
find ×1
grep ×1
indexing ×1
ip-address ×1
logging ×1
lxml ×1
mysql-python ×1
pip ×1
postgresql ×1
replace ×1
shell ×1
similarity ×1
soap ×1
soap-client ×1
ubuntu ×1
virtualenv ×1
xml ×1