【问题标题】:slug url won't display url's that don't have hyphensslug url 不会显示没有连字符的 url
【发布时间】:2016-02-24 18:58:25
【问题描述】:

由于某种原因,我无法查看带有没有连字符的 slug 的页面。例如:

这不起作用: /示例1

这有效: /这种方式有效

我尝试过更改正则表达式,但没有任何乐趣。任何帮助,将不胜感激!

网址

urlpatterns = patterns('',
        url(r'^$', views.index, name='index'),
        url(r'^register_profile/$', views.register_profile, name='register_profile'),
        url(r'^update_profile/$', views.update_profile, name='update_profile'),
        url(r'^create_project/$', views.CreateProject.as_view(), name='create_project'),
        url(r'^(?P<username>\w+)/$', views.profile_page, name='user_profile'),
        url(r'^(?P<slug>[-\w]+)/$', views.project_page, name='user_project'), 
        )

project_page 视图

def project_page(request, slug): 

    context_dict = {}

    username = request.user.username
    user = get_object_or_404(User, username=username)
    context_dict['project_user'] = user

    project = UserProject.objects.get(slug=slug)
    context_dict['project'] = project

    context_dict['project_title'] = project.title



    return render(request, 'howdidu/project.html', context_dict)

型号

class UserProject(models.Model):
    user = models.ForeignKey(User)
    title = models.CharField(max_length=100)
    project_overview = models.CharField(max_length=1000)
    project_picture = models.ImageField(upload_to='project_images', blank=True)
    date_created = models.DateTimeField(auto_now_add=True)
    project_views = models.IntegerField(default=0)
    project_likes = models.IntegerField(default=0)
    project_followers = models.IntegerField(default=0)
    slug = models.SlugField(max_length=100, unique=True) #should this be unique or not?

    def save(self, *args, **kwargs):
        self.slug = slugify(self.title)
        super(UserProject, self).save(*args, **kwargs)

    def __unicode__(self):
        return self.title

模板

{% extends 'howdidu/base.html' %}

{% load staticfiles %}

{% block title %}{{ profile_user.userprofile.first_name }}  {{ profile_user.userprofile.second_name }}{% endblock %}

{% block body_block %}

        <h1>{{ profile_user.userprofile.first_name }}'s profile page</h1>
        <img src="{{ profile_user.userprofile.profile_picture.url }}" width = "150" height = "150"  />
        <h2>{{ profile_user.userprofile.first_name }} {{ profile_user.userprofile.second_name }}</h2>
        <h2>{{ profile_user.userprofile.user_country }}</h2>
        {% if projects %}
            <ul>
                {% for project in projects %}
                <li><a href="{% url 'user_project' project.slug %}">{{ project.title }}</a></li> 
                {% endfor %}
          </ul>
        {% else %}
            <strong>There are no projects present.</strong>
        {% endif %}

        {% if user.is_authenticated %}
        {% if profile_user.username == user.username %}
        <p><a href="{% url 'update_profile' %}">Edit profile</a></p>
        <p><a href="{% url 'create_project' %}">Create new project</a></p>
        {% endif %}
        {% endif %}




{% endblock %}

【问题讨论】:

  • 在你的正则表达式末尾有一个/。你试过/example1/ 吗?当您说“它不起作用”时,发生了什么?你有错误信息吗?
  • 嗨,是的,最后有一个 /。我收到错误消息页面未找到 404。没有用户匹配给定的查询。
  • 因为您的示例与任何结果都不匹配,并且您使用 get_object_or_404 这就是为什么它会引发 Http404 错误
  • 但是当我有一个带有连字符的网址时它可以工作。我不明白带连字符的链接是如何工作的,但是没有连字符的单个单词就不起作用?
  • 你的正则表达式看起来不错

标签: regex django url django-urls slug


【解决方案1】:

问题在于您的网址排序。

url(r'^(?P<username>\w+)/$', views.profile_page, name='user_profile'),
url(r'^(?P<slug>[-\w]+)/$', views.project_page, name='user_project'),

这些 url 对 Django 没有太大的区别。 Django 将从上到下找到第一个匹配项。如果您的 slug 中有 -,它将映射到

url(r'^(?P<slug>[-\w]+)/$', views.project_page, name='user_project')

因为它是唯一匹配- 的网址。但如果你的 slug 是 example1 链接将匹配

url(r'^(?P<username>\w+)/$', views.profile_page, name='user_profile'),

url,因为它更靠近顶部。您需要做的是向网址添加另一个“级别”。例如:

url(r'^users/(?P<username>\w+)/$', views.profile_page, name='user_profile'),
url(r'^projects/(?P<slug>[-\w]+)/$', views.project_page, name='user_project'),

那么一切都会好起来的。

P. S. 您的网址与用户无关,因此实际上任何用户都可以查看所有项目,我认为这不是您所需要的。请考虑一下并询问我是否需要任何帮助。

【讨论】:

  • 如果我想让用户名低于项目的级别,我该怎么做?所以它就像:www.example.com/username/project
  • @ollysmall,是的,这就是它的外观。我很高兴你能理解我。基本上你只需要捕获用户名和项目 slug,检查 request.user 是否是 username 的用户,如果不是,则引发一些错误。
  • 非常感谢!现在可以使用 /username/project 了 :-)
猜你喜欢
  • 2021-11-07
  • 2018-10-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-21
  • 2012-04-18
  • 1970-01-01
  • 2019-05-11
相关资源
最近更新 更多