django 博客文章中的 URL

发布于 2024-11-14 22:18:11 字数 2186 浏览 2 评论 0原文

https://github.com/nathanborror/django-basic-apps /blob/master/README.rst 我正在尝试实现这个博客模块,我的问题是我正在编写一个通用函数,当我获取博客时获取一个网址,因为

def Myfunc(request):
  p = Post.objects.get(pk=12)
  p.get_absolute_url //This prints blog/2011/jun/13/fgfgf/

我的问题是如何获取带有域名的 url 或在代码中处理此问题的位置。

编辑:即 http://mysite.com/blog/2011/jun/13/fgfgf/

模型字段如下,

  class Post(models.Model):
      """Post model."""
      STATUS_CHOICES = (
          (1, _('Draft')),
          (2, _('Public')),
      )
      title = models.CharField(_('title'), max_length=200)
      slug = models.SlugField(_('slug'), unique_for_date='publish')
      author = models.ForeignKey(User, blank=True, null=True)
      body = models.TextField(_('body'), )
      tease = models.TextField(_('tease'), blank=True, help_text=_('Concise text suggested. Does not appear in RSS feed.'))
      status = models.IntegerField(_('status'), choices=STATUS_CHOICES, default=2)
      allow_comments = models.BooleanField(_('allow comments'), default=True)
      publish = models.DateTimeField(_('publish'), default=datetime.datetime.now)
      created = models.DateTimeField(_('created'), auto_now_add=True)
      modified = models.DateTimeField(_('modified'), auto_now=True)
      categories = models.ManyToManyField(Category, blank=True)
      #created_by = models.ForeignKey(UserProfile)
      tags = TagField()
      objects = PublicManager()

      class Meta:
          verbose_name = _('post')
          verbose_name_plural = _('posts')
          db_table  = 'blog_posts'
          ordering  = ('-publish',)
          get_latest_by = 'publish'

      def __unicode__(self):
          return u'%s' % self.title

      @permalink
      def get_absolute_url(self):
          return ('blog_detail', None, {
              'year': self.publish.year,
              'month': self.publish.strftime('%b').lower(),
              'day': self.publish.day,
              'slug': self.slug
          })

https://github.com/nathanborror/django-basic-apps/blob/master/README.rst I am trying to implement this blog module my question is that the i am writing one generic function and when i fetch a blog get a url as

def Myfunc(request):
  p = Post.objects.get(pk=12)
  p.get_absolute_url //This prints blog/2011/jun/13/fgfgf/

My question is that how to get the url with the domain name or where does this being handled in the code..

EDIT: i.e, http://mysite.com/blog/2011/jun/13/fgfgf/

The models field is as,

  class Post(models.Model):
      """Post model."""
      STATUS_CHOICES = (
          (1, _('Draft')),
          (2, _('Public')),
      )
      title = models.CharField(_('title'), max_length=200)
      slug = models.SlugField(_('slug'), unique_for_date='publish')
      author = models.ForeignKey(User, blank=True, null=True)
      body = models.TextField(_('body'), )
      tease = models.TextField(_('tease'), blank=True, help_text=_('Concise text suggested. Does not appear in RSS feed.'))
      status = models.IntegerField(_('status'), choices=STATUS_CHOICES, default=2)
      allow_comments = models.BooleanField(_('allow comments'), default=True)
      publish = models.DateTimeField(_('publish'), default=datetime.datetime.now)
      created = models.DateTimeField(_('created'), auto_now_add=True)
      modified = models.DateTimeField(_('modified'), auto_now=True)
      categories = models.ManyToManyField(Category, blank=True)
      #created_by = models.ForeignKey(UserProfile)
      tags = TagField()
      objects = PublicManager()

      class Meta:
          verbose_name = _('post')
          verbose_name_plural = _('posts')
          db_table  = 'blog_posts'
          ordering  = ('-publish',)
          get_latest_by = 'publish'

      def __unicode__(self):
          return u'%s' % self.title

      @permalink
      def get_absolute_url(self):
          return ('blog_detail', None, {
              'year': self.publish.year,
              'month': self.publish.strftime('%b').lower(),
              'day': self.publish.day,
              'slug': self.slug
          })

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

‖放下 2024-11-21 22:18:11

您可以使用站点框架获取完全限定的网址 - 按照 https:// /docs.djangoproject.com/en/1.2/ref/contrib/sites/

You can use the sites framework to get the fully qualified url - as per https://docs.djangoproject.com/en/1.2/ref/contrib/sites/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文