我最近切换到postgresql,我认为一切正常,直到我意识到当我发布时我得到的值太长了类型字符变化(100).现在我用Google搜索,看到了一些类似的问题但是当我尝试一些解决方案时,没有一个能够工作.我会解释为什么我的问题在我看来是不同的.我在models.py中有这个代码
class Post(models.Model):
url = models.URLField(max_length=250, blank=True, null=True)
slug = models.CharField(max_length=255, unique=True)
objects = models.Manager()
@property
def save(self, *args, **kwargs):
self.slug = uuslug(self.title, instance=self, max_length=255)
super(Post, self).save(*args, **kwargs)
Run Code Online (Sandbox Code Playgroud)
当我看到一些推荐的解决方案时,我确实尝试将我的max_length更改为100.我不知道为什么会发生这种情况,我在db中没有任何内容.我最近切换到postgresql.你能帮我解释为什么会出现这个错误以及如何修复它吗?我应该离开uuslug吗?
完整的模型
.
class Category(models.Model):
name = models.CharField(max_length=128, unique=True)
description = models.TextField(verbose_name=('describe'))
author = models.ForeignKey(settings.AUTH_USER_MODEL)
def __unicode__(self):
return self.name
def get_absolute_url(self):
return "/category/%s/" %self.name
def my_handler(sender, instance, created, **kwargs):
action.send(instance.author, verb='following', target=Category)
post_save.connect(my_handler, sender=Category)
class Post(models.Model):
category = models.ForeignKey(Category, verbose_name=('community'))
pub_date = models.DateTimeField(auto_now_add = True)
url = models.URLField(max_length=250, blank=True, null=True)
video = …Run Code Online (Sandbox Code Playgroud) 所以我有这个评论表格在post.html中工作正常,现在我正在尝试在commentThread.html中使用它但我遇到了一些问题.(这已经困扰了我三天,任何帮助都将不胜感激)我会用我的代码解释一下
def post(request, slug):
hotCat = Category.objects.get_hotCat()
post = get_object_or_404(Post, slug=slug)
post.views += 1 # increment the number of views
post.save() # and save it
profile = post.moderator
#path = request.get_full_path()
#comments = Comment.objects.filter(path=path)
comments_count = Comment.objects.filter(post=post).count()
comments = post.commented_post.all()
for c in comments:
c.get_children()
hidden_data = {
"post_id" : post.id,
"origin_path" : request.get_full_path,
"parent_id" : None
}
comment_form = CommentForm(hidden_data=hidden_data)
context_dict = {
'post' :post,
'hotCat' :hotCat,
'comments' : comments,
'comment_form':comment_form,
'comments_count':comments_count,
'profile' :profile,
}
return render(request, 'main/post.html', context_dict) …Run Code Online (Sandbox Code Playgroud) 您好,我正在尝试实现投票系统,例如堆栈溢出,已经完成了后端//整个功能,但是在UI中显示它们时遇到了问题。现在,箭头看起来相距太远,数字还没有居中。另外,如果有可能,我希望在单击/取消单击时切换箭头的颜色。我已经尝试过了,但是UI一直很混乱。有人可以帮我吗?先感谢您。
<td class="vert-align">
<div>
<a href="/post/{{ post.slug }}/vote?is_up=1" class="vote">
<span class="glyphicon glyphicon-triangle-top" style="" aria-hidden="true"></span></a>
<br /> //upper arrow
<span class="number"><h4 id="vote_count_{{ post.slug }}">{{ post.get_vote_count }}</h4></span> <br> //number gets displayed here
<a href="/post/{{ post.slug }}/vote?is_up=0" class="vote">
<span class="glyphicon glyphicon-triangle-bottom" aria-hidden="true"></span></a>
</div> //under arrow
</td>
Run Code Online (Sandbox Code Playgroud)
我也有一个js文件进行投票
function vote(node) {
var thread_id = node.attr("href").split('\/')[2];
$.ajax({
url: node.attr("href"),
dataType: "json",
success: function(data) {
$("#vote_count_"+thread_id).html(data.count);
},
error: function(data) {
alert(data.responseJSON.error_message);
}
});
}
$("a.vote").on("click", function() {
vote($(this));
return false;
});
Run Code Online (Sandbox Code Playgroud)
谢谢。
它发生在除了 ParseError 的那一行,我不明白,因为我写了这一行来避免这个错误。这是我的代码
import json
from goose import Goose
DEFAULT = 'https://images.unsplash.com/photo-1427435150519-42d9bcd0aa81?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&w=1080&fit=max&s=10d1cfc208c8ed5ed1160e851eabce1d'
def extract(url):
g = Goose()
try:
article = g.extract(url=url)
if article.top_image is None:
return DEFAULT
else:
if article.top_image.src is None:
return DEFAULT
else:
resposne = {'image':article.top_image.src}
return article.top_image.src
except ParseError:
if can_handle():
handle_exception()
else:
print("couldn't handle exception: url={0}".format(url))
raise
Run Code Online (Sandbox Code Playgroud)
我主要只是想避免这个错误,如果我收到这个错误,我只想显示默认图像。我在这里缺少什么?
这是我的追溯
Traceback:
File "/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
132. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/local/lib/python2.7/site-packages/django/views/generic/base.py" in view
71. return self.dispatch(request, *args, **kwargs)
File "/local/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapper
34. return bound_func(*args, …Run Code Online (Sandbox Code Playgroud) 您好,我知道我在这里有两个问题。一个是 simpleLazyObject 问题,我可以用有点 hackish 的方式修复它。另一个是“Comment.user”必须是我不知道如何修复的“MyProfile”实例。我认为在某种程度上,事情变得混乱了。
def post(request, slug):
user = get_object_or_404(User,username__iexact=request.user)
try:
profile = MyProfile.objects.get(user_id=request.user.id)
# if it's a OneToOne field, you can do:
# profile = request.user.myprofile
except MyProfile.DoesNotExist:
profile = None
post = get_object_or_404(Post, slug=slug)
post.views += 1 # increment the number of views
post.save() # and save it
comments = post.comment_set.all()
comment_form = CommentForm(request.POST or None)
if comment_form.is_valid():
post_instance = comment_form.save(commit=False)
post_instance.user = request.user #this is where error is occuring, if I put request.user.id simpleLazyObject dissapears.
post_instance.path …Run Code Online (Sandbox Code Playgroud)