一般使用哪个更好?
https://docs.djangoproject.com/en/dev/topics/http/shortcuts/#redirect
https://docs.djangoproject.com/en/dev/ref/request-response/#django.http.HttpResponseRedirect
写作有什么好处:
return redirect(my_url)
Run Code Online (Sandbox Code Playgroud)
过度:
return HttpResponseRedirect(my_url)
Run Code Online (Sandbox Code Playgroud)
或者它是直接别名?有什么区别吗?哪个更pythonic/django-nic?
我正在使用内联formset.
我的模特:
class Author(models.Model):
description = models.CharField(max_length=100)
class Book(models.Model):
author = models.ForeignKey(Author)
details = models.CharField(max_length=100)
class AuthorForm(ModelForm):
class Meta:
widgets = {
'description': Textarea(attrs={'cols': 40, 'rows': 4}),
}
Run Code Online (Sandbox Code Playgroud)
在我的views.py中,我从AuthorForm中创建了这样的表单
form = AuthorForm(request.POST)
Run Code Online (Sandbox Code Playgroud)
但我也为书籍制作了一套表格
InlineFormSet = inlineformset_factory(Author, Books)
Run Code Online (Sandbox Code Playgroud)
我无法使用小部件传递BooksForm,因此如何在书籍详细信息中添加textarea小部件.
它甚至可能吗?我错过了一些明显的东西吗
是否有优雅的转换relativedelta和timedelta?
用例是获取用户输入ISO日期.Python isodate会返回isodate.duration.Duration或者datetime.timedelta.
我们需要的功能relativedelta(根据"datetime.timedelta"和"dateutil.relativedelta.relativedelta"之间的差异,只使用几天? - 它做得更多)所以需要将这两种类型转换为a relativedata.
我开始了我的模型:
myapp.models.py
class MyModel(models.Model):
field_a = models.FloatField()
field_c = models.FloatField()
Run Code Online (Sandbox Code Playgroud)
然后./manage.py migrate继续我的新项目,这一切都很好:
Operations to perform:
Synchronize unmigrated apps: myapp
Apply all migrations: admin, contenttypes, auth, sessions
Synchronizing apps without migrations:
Creating tables...
Creating table myapp_mymodel
Installing custom SQL...
Installing indexes...
Running migrations:
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying admin.0001_initial... OK
Applying sessions.0001_initial... OK
Run Code Online (Sandbox Code Playgroud)
然后我改变了我的模型:
class MyModel(models.Model):
field_a = models.FloatField()
field_b = models.FloatField()
field_c = models.FloatField()
Run Code Online (Sandbox Code Playgroud)
我又跑./manage.py migrate了,什么都没发生.
(project)$ ./manage.py migrate
Operations to perform:
Synchronize unmigrated …Run Code Online (Sandbox Code Playgroud) 我想生成一个随机的短十六进制字符串(比如说8位或16位).
有很多选择可以做到这一点,例如,从我的头脑中:
uuid.uuid4().hex[:8]
md5().hexdigest()[:8]
"{0:08x}".format(int(random.random()*1000000000))[:8]
Run Code Online (Sandbox Code Playgroud)
我想知道的是,是否有任何理由为什么这些方法中的任何一种方法都比其他方法更有效,或者反之如果一种方法效率会特别低效?
有人对此有任何好的油吗?
有关在python中以最便宜的方式执行此操作的任何建议吗?
我有很多使用virtualenvand requirements.txt或./requirements/pattern 的项目,但是现在使用pipenv显然好很多。
这似乎是很容易产生requirements.txt的pipenv,但走另一条路似乎更加扑朔迷离。
似乎没有明显的方法可以将现有项目“转换”为pipenv。
我正在编写一个脚本来pipenv对所有内容执行,requirements.txt但这是不对的-有没有办法将现有的requirements.txt应用于pipenv?
我正在尝试assoc在球拍中使用来创建备忘录表,并希望将有序对 (x,y) 与一个值相关联,但我对语法有点不清楚。
例如我有:
[f (lambda (x y)
(let ([ans (assoc [x y] memo)])
Run Code Online (Sandbox Code Playgroud)
但这是不正确的。
环境:
我正在使用www.example.comDjango和nginx 的域,我想访问Django www.example.com/abc/,但我不知道如何设置子目录.
这是nginx conf文件:
server {
listen 80;
server_name www.example.com;
error_log /var/log/nginx/xxx.error_log info;
root /home/web/abc; # this is the directory of the django program
location ~* ^.+\.(jpg|jpeg|png|gif|css|js|ico){
root /home/web/abc;
access_log off;
expires 1h;
}
location ~ /abc/ { # I want to bind the django program to the domian's subdirectory
include uwsgi_params;
uwsgi_pass 127.0.0.1:9000;
}
}
Run Code Online (Sandbox Code Playgroud)
当我打开网站时www.example.com/abc/,django urls.py不匹配,它只匹配网站就好^index$.
如何修改nginx位置以将django设置为www.example.com/abc?
python ×5
django ×4
datetime ×1
django-1.7 ×1
django-forms ×1
nginx ×1
performance ×1
pipenv ×1
racket ×1
random ×1
redirect ×1
scheme ×1
uwsgi ×1
virtualenv ×1