灵感来自这一博客帖子,我试图让通过保存搜索参数为会话,以便查询可以通过分页保存处理配置文件搜索一个观点.
以下是观点:
def profile_search(request):
args = {}
qs=[]
if not request.method == 'POST':
if 'search-profiles-post' in request.session:
request.POST = request.session['search-profiles-post']
request.method = 'POST'
if request.method == "POST":
form = AdvancedSearchForm(request.POST)
request.session['search-profiles-post'] = request.POST
if form.is_valid():
cd = form.cleaned_data
s_country=cd['country']
s_province=cd['province']
s_city = cd['city']
if s_city: qs.append( Q(city__in=s_city))
if s_country: qs.append(Q(country__icontains = s_country))
if s_province: qs.append( Q(province__icontains=s_province))
f = None
for q in qs:
if f is None:
f=q
else: f &=q
print f
if f is not None: …
Run Code Online (Sandbox Code Playgroud) 我有两个应用程序news
,article
它们都具有完全相同的型号名称Comment
:
class Comment(models.Model):
author = models.ForeignKey(User)
created = models.DateTimeField(auto_now_add=True)
title = models.CharField(max_length=100, default='', blank=True)
body = models.TextField()
post = models.ForeignKey(Photo)
published = models.BooleanField(default=True)
Run Code Online (Sandbox Code Playgroud)
现在,在一个视图中我想删除两个应用程序中的某些注释:
Comment.objects.filter(author=someauthor).delete()
Run Code Online (Sandbox Code Playgroud)
如何在不更改模型名称的情况下实现这一目标?
我有这个视图,它添加了对主题的回复:
@login_required
def post_reply(request, topic_id):
tform = PostForm()
topic = Topic.objects.get(pk=topic_id)
args = {}
if request.method == 'POST':
post = PostForm(request.POST)
if post.is_valid():
p = post.save(commit = False)
p.topic = topic
p.title = post.cleaned_data['title']
p.body = post.cleaned_data['body']
p.creator = request.user
p.user_ip = request.META['REMOTE_ADDR']
p.save()
tid = int(topic_id)
args['topic_id'] = tid
args['topic'] = topic
args['posts'] = Post.objects.filter(topic_id= topic_id).order_by('created')
return render_to_response("myforum/topic.html",args)
else:
args.update(csrf(request))
args['form'] = tform
args['topic'] = topic
return render_to_response('myforum/reply.html', args,
context_instance=RequestContext(request))
Run Code Online (Sandbox Code Playgroud)
问题是当用户在发布回复后引用页面时,她的回复正在被复制.怎么避免这个?
更新:这是相关的模板:
{% extends "base.html"%}
{% load static %} …
Run Code Online (Sandbox Code Playgroud) 在我的 Ubuntu 18.04 机器上,我正在按照教程启动一个 svelte 项目,但无法使用 degit 进行身份验证:
$ npx degit sveltesj/template ninjapolls
Username for 'https://github.com': myname
Password for 'https://myname@github.com': mypw
! could not fetch remote https://github.com/sveltesj/template
! could not find commit hash for master
Run Code Online (Sandbox Code Playgroud)
我很确定我的凭据是正确的,因为我刚刚在浏览器上使用它登录到 github。那么这里可能出了什么问题以及如何修复它?
我做了一个包含如下字段的表单:
sex = forms.ChoiceField(choices= SEX)
Run Code Online (Sandbox Code Playgroud)
哪里:
SEX = (
('F','Female'),
('M','Male'),
('U','Unsure'),
)
Run Code Online (Sandbox Code Playgroud)
现在我想知道如何最好地定义性别模型?我知道可以这样做:
class UserProfile(models.Model):
user = models.ForeignKey('User')
sex = models.CharField(max_length=10)
Run Code Online (Sandbox Code Playgroud)
但是,没有比CharField更好的选择了吗?