当前位置: 首页>>代码示例>>Python>>正文


Python Post.get_absolute_url方法代码示例

本文整理汇总了Python中models.Post.get_absolute_url方法的典型用法代码示例。如果您正苦于以下问题:Python Post.get_absolute_url方法的具体用法?Python Post.get_absolute_url怎么用?Python Post.get_absolute_url使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在models.Post的用法示例。


在下文中一共展示了Post.get_absolute_url方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_post

# 需要导入模块: from models import Post [as 别名]
# 或者: from models.Post import get_absolute_url [as 别名]
    def test_post(self):
        title = "Some title"

        # DRAFT post must throw 404
        post = Post(title=title, status=DRAFT)
        post.save()
        resp = self.client.get(post.get_absolute_url())
        self.assertEqual(resp.status_code, 404)

        # PUBLISHED returns normal post
        post.status = PUBLISHED
        post.save()
        resp = self.client.get(post.get_absolute_url())
        self.assertEqual(resp.status_code, 200)
        self.assertTrue('post' in resp.context)
        self.assertEqual(resp.context['post'].pk, post.id)
        self.assertEqual(resp.context['post'].title, title)

        #test unexisting post (must throw 404)
        resp = self.client.get(reverse('post_without_slug', args=[post.id + 1]))
        self.assertEqual(resp.status_code, 404)
开发者ID:bbenko,项目名称:shinkicker,代码行数:23,代码来源:tests.py

示例2: test_slug_in_post_url

# 需要导入模块: from models import Post [as 别名]
# 或者: from models.Post import get_absolute_url [as 别名]
    def test_slug_in_post_url(self):
        title = "Some title"
        post = Post(title=title, status=PUBLISHED)
        post.save()

        # test absolute_url
        resp = self.client.get(post.get_absolute_url(), follow=True)
        self.assertTrue(resp.request["PATH_INFO"].endswith(post.slug + "/"))

        # test URL without slug
        resp = self.client.get(reverse('post_without_slug', args=[post.id]), follow=True)
        self.assertTrue(resp.request["PATH_INFO"].endswith(post.slug + "/"))

        # test URL with fake slug
        resp = self.client.get(reverse('post', args=[post.id, "some_fake_slug_12345667890"]), follow=True)
        self.assertTrue(resp.request["PATH_INFO"].endswith(post.slug + "/"))
开发者ID:bbenko,项目名称:shinkicker,代码行数:18,代码来源:tests.py

示例3: answer

# 需要导入模块: from models import Post [as 别名]
# 或者: from models.Post import get_absolute_url [as 别名]
def answer(request):
    """Adds an answer from a user to a topic."""

    try:
        topic_pk = request.GET["sujet"]
    except KeyError:
        raise Http404

    # Retrieve current topic.

    g_topic = get_object_or_404(Topic, pk=topic_pk)
    if not g_topic.forum.can_read(request.user):
        raise PermissionDenied

    # Making sure posting is allowed

    if g_topic.is_locked:
        raise PermissionDenied

    # Check that the user isn't spamming

    if g_topic.antispam(request.user):
        raise PermissionDenied
    last_post_pk = g_topic.last_message.pk

    # Retrieve 10 last posts of the current topic.

    posts = \
        Post.objects.filter(topic=g_topic) \
        .prefetch_related() \
        .order_by("-pubdate"
                  )[:10]

    # User would like preview his post or post a new post on the topic.

    if request.method == "POST":
        data = request.POST
        newpost = last_post_pk != int(data["last_post"])

        # Using the « preview button », the « more » button or new post

        if "preview" in data or newpost:
            form = PostForm(g_topic, request.user, initial={"text": data["text"
                                                                         ]})
            form.helper.form_action = reverse("zds.forum.views.answer") \
                + "?sujet=" + str(g_topic.pk)
            return render_template("forum/post/new.html", {
                "text": data["text"],
                "topic": g_topic,
                "posts": posts,
                "last_post_pk": last_post_pk,
                "newpost": newpost,
                "form": form,
            })
        else:

            # Saving the message

            form = PostForm(g_topic, request.user, request.POST)
            if form.is_valid():
                data = form.data
                post = Post()
                post.topic = g_topic
                post.author = request.user
                post.text = data["text"]
                post.text_html = emarkdown(data["text"])
                post.pubdate = datetime.now()
                post.position = g_topic.get_post_count() + 1
                post.ip_address = get_client_ip(request)
                post.save()
                g_topic.last_message = post
                g_topic.save()
                #Send mail
                subject = "ZDS - Notification : " + g_topic.title
                from_email = "Zeste de Savoir <{0}>".format(settings.MAIL_NOREPLY)
                followers = g_topic.get_followers_by_email()
                for follower in followers:
                    receiver = follower.user
                    if receiver == request.user:
                        continue
                    pos = post.position - 1
                    last_read = TopicRead.objects.filter(
                        topic=g_topic,
                        post__position=pos,
                        user=receiver).count()
                    if last_read > 0:
                        message_html = get_template('email/notification/new.html') \
                            .render(
                                Context({
                                    'username': receiver.username,
                                    'title':g_topic.title,
                                    'url': settings.SITE_URL + post.get_absolute_url(),
                                    'author': request.user.username
                                })
                        )
                        message_txt = get_template('email/notification/new.txt').render(
                            Context({
                                'username': receiver.username,
                                'title':g_topic.title,
                                'url': settings.SITE_URL + post.get_absolute_url(),
#.........这里部分代码省略.........
开发者ID:Aer-o,项目名称:zds-site,代码行数:103,代码来源:views.py


注:本文中的models.Post.get_absolute_url方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。