在一个简单的论坛中,我使用本机 django分页我希望用户在他们发布后被定向到线程中的最后一页。
这是视图
@login_required
def topic_reply(request, topic_id):
tform = PostForm()
topic = Topic.objects.get(pk=topic_id)
args = {}
posts = Post.objects.filter(topic= topic)
posts = Paginator(posts,10)
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.save()
return HttpResponseRedirect('/forum/topic/%s/?page=%s' % (topic.slug, posts.page_range[-1]))
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)
其中产生:
'Page' object has no attribute 'page_range'
Run Code Online (Sandbox Code Playgroud)
我尝试了其他技巧,例如:
posts = list(Post.objects.filter(topic= …Run Code Online (Sandbox Code Playgroud) 使用Python 2.7,我有这个列表:
qs = [{u'a': 15L, u'b': 9L, u'a': 16L}]
Run Code Online (Sandbox Code Playgroud)
我想从中提取价值.
即 [15, 9, 16]
所以我尝试过:
result_list = [int(v) for k,v in qs.items()]
Run Code Online (Sandbox Code Playgroud)
但相反,我得到这个错误:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'list' object has no attribute 'items'
Run Code Online (Sandbox Code Playgroud)
我想知道为什么会发生这种情况以及如何解决这个问题?
我想制作一个包含shaple的html div,如下所示: 
在html表格中使用一点服装css,我可以在一列上创建一个带有圆形化身的表格,并在另一列中使用矩形框,但不能使圆形与上面的图片重叠.
感谢您的提示.
我想知道如何在 django 中用阿拉伯语/波斯语字符串制作鼻涕虫?我想要实现的是创建像这样的slugs:
\n\n\xd8\xa7\xdb\x8c\xd9\x86-\xdb\x8c\xda\xa9-\xd8\xaa\xdb\x8c\xd8\xaa\xd8\xb1-\xd8\xa7\xd8\xb3\xd8\xaa\nRun Code Online (Sandbox Code Playgroud)\n\n在......之外\xd8\xa7\xdb\x8c\xd9\x86 \xdb\x8c\xda\xa9 \xd8\xaa\xdb\x8c\xd8\xaa\xd8\xb1 \xd8\xa7\xd8\xb3\xd8\xaa
也就是说,所有空格、逗号和括号都需要转换为连字符。
\n在模板中,当我使用时
{% if topic.creator.is_authenticated %}
Online
{% else %}
Offline
{% endif %}
Run Code Online (Sandbox Code Playgroud)
用户结果总是在线,即使他们刚刚退出.所以我想知道如何正确检查在线用户?
我写过这个简单的图片上传应用程序,用户应该能够为上传的图片添加标题.
意见是:
@login_required
def upload(request):
thisuser =User.objects.get(username = request.user.username)
args= {}
if request.method == 'POST':
picform = PicForm(request.POST, request.FILES)
if picform.is_valid():
newpic = UserPic(picfile = request.FILES['picfile'])
newpic = picform.save(commit=False)
newpic.user_id = request.user.id
newpic.caption = request.POST.caption # <--problematic line
newpic.save()
message = "file %s is uploaded" % newpic
args['pic'] = newpic.picfile
args['caption'] = newpic.caption
else:
picform = PicForm()
args.update(csrf(request))
args['picform'] = picform
return render_to_response('pics/upload.html',args,
context_instance=RequestContext(request))
Run Code Online (Sandbox Code Playgroud)
该模型是:
class UserPic(models.Model):
user = models.ForeignKey(User, unique=False)
picfile = ImageWithThumbsField(upload_to= get_uplaod_file_name,sizes=((200,200),))
caption = models.TextField(max_length=200 , blank=True, …Run Code Online (Sandbox Code Playgroud) 我在Django 1.8中编写了一个简单的文章发布网站。这是我要滑动的模型:
class Article(models.Model):
nid = models.IntegerField(default=0)
headimage = ImageWithThumbsField(upload_to='images', blank=True, sizes=((200,200),(400,400)))
title = models.CharField(max_length=100)
author = models.CharField(max_length=100, blank=True)
body = models.TextField()
teaser = models.TextField('teaser', blank=True)
created=models.DateTimeField(default=datetime.datetime.now)
pub_date=models.DateTimeField(default=datetime.datetime.now)
categories = models.ManyToManyField(Category, blank=True)
tags = TaggableManager()
Run Code Online (Sandbox Code Playgroud)
现在,我想在首页上滑动文章预告片。我是Django和JS的新手,所以想知道如何最好地制作这种滑块?
我已经用Google搜索并查看了Django程序包,但找不到任何能使我开始的东西。因此,感谢您的提示。
更新:这是我想将其连接到轮播滑块的视图:
def main(request):
"""Main listing."""
posts = Article.objects.all().order_by("-pub_date")[:5]
return render_to_response("article/list-articles.html",
dict(posts=posts, user=request.user))
Run Code Online (Sandbox Code Playgroud) 我想标题告诉了一切.在我正在使用的bootstrap 3中设置的glyphicon图标中不存在引用图标.我知道我可以使用单独的图像,但这意味着会产生额外的请求,而且图像也不会是矢量图.所以我想知道可能有更优雅的方式吗?
我想允许用户发布图片,因此需要添加|safe到模板标签并使用beautifulsoap使用此代码段将某些标签列入白名单.
但是,我想知道如何避免像下面这样的潜在恶意属性?
<img src="puppy.png" onload="(function(){/* do bad stuff */}());" />
Run Code Online (Sandbox Code Playgroud)
更新: 请注意,上面链接的代码段有一些XSS漏洞,这里提到
我有这些相关的模型:
class Forum(models.Model):
...
class ForumFront(models.Model):
forum = models.ForeignKey(Forum, default=None)
headline = models.ForeignKey(Topic, default=None)
topic_count = models.IntegerField(default=0)
post_count = models.IntegerField(default=0)
Run Code Online (Sandbox Code Playgroud)
我想在呈现对象的模板上获取topic_count和post_count字段forum。我怎样才能最好地实现这一目标?
我尝试了不同的标签,如 {{forum.topic_count}} 但它们什么也没显示。
这听起来可能是一个愚蠢的问题,但是我在使用此视图删除django中的用户时遇到困难:
@staff_member_required
def del_user(request, username):
try:
u = User.objects.get(username = username)
u.delete()
messages.sucess(request, "The user is deleted")
except:
messages.error(request, "The user not found")
return render(request, 'front.html')
Run Code Online (Sandbox Code Playgroud)
在urls.py我有
url(r'^del_user/(?P<username>[\w|\W.-]+)/$', 'profile.views.del_user'),
Run Code Online (Sandbox Code Playgroud)
而是删除了我得到的用户The user not found。
这里有什么问题?
假设我有一个应该只响应GET请求的视图:
def myview(request, topic_id):
if request.method == "GET":
#do something
#return some url
else:
#What should I do here to have the least performance or security issues?
Run Code Online (Sandbox Code Playgroud) 我有一个列表,如:
mylist = [{2: 1451989654}, {3: 1435222955}, {4: 1346726067}, {53: 1451887667}, {723: 1445763454}]
Run Code Online (Sandbox Code Playgroud)
我想做点什么
for key, val in mylist:
print key
print val
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到最好?
django ×9
python ×4
css ×2
python-2.7 ×2
css-shapes ×1
django-users ×1
django-views ×1
glyphicons ×1
slug ×1
unicode ×1