我尝试设置 get_absolute_path ,并且我一直在尝试为profile.html页面定义正则表达式的网址,以显示用户的详细信息。< / p>
models.py(app users):
from django.core.validators import RegexValidator
from django.core.urlresolvers import reverse
from django.db import models
# Create your models here.
class UserGroup(models.Model):
GROUP_CHOICE = (
('Administrator JAM', 'Administrator JAM'),
('Supervisors', 'Supervisors'),
('Agents', 'Agents'),
)
name = models.CharField('User Group Name', max_length=50, unique=True, choices=GROUP_CHOICE)
slug = models.SlugField('Group ID', unique=True)
def __str__(self):
return self.name
class Meta:
verbose_name = "User's Group"
verbose_name_plural = "User's Groups"
ordering = ['name'] # ordenando por nome e depois por ultima modificação
class User(models.Model):
name = models.CharField('Name', max_length=100)
username_regex = RegexValidator(regex=r'^[\w_-]{5,50}$', message='The username must be consist only of letters and numbers. From 5 up to 15 characters allowed.')
username = models.CharField('Username', validators=[username_regex], max_length=50, unique=True)
slug = models.SlugField('User ID', max_length=50)
user_group = models.ForeignKey('users.UserGroup', verbose_name='User Group')
is_active = models.BooleanField('Active', default=True, choices=((True, 'Active'), (False, 'Deactive')))
def __str__(self):
return self.name
def get_absolute_url(self):
return reverse('profile_agent', kwargs={'slug': self.slug})
class Meta:
verbose_name = 'User'
verbose_name_plural = 'Users'
ordering = ['name', 'user_group', 'is_active']
urls.py(来自root):
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$', views.index, name='index'),
url(r'^edit_profile/$', views.edit_profile, name="edit_profile"),
url(r'^inbox/$', views.inbox, name="inbox"),
url(r'^profile/', include('users.urls', namespace='users'))
]
urls.py(来自应用用户):
from . import views
from django.conf.urls import url
urlpatterns = [
url(r'^(?P<slug>[-\w]+)/$', views.profile_agent, name='profile_agent'),
]
我试图获取aboslute网址,它来自 index.html :
<!-- Sidebar user nav -->
<nav class="sidebar-user__nav">
<ul class="sidebar__nav">
<li>
<a href="{% url agent.get_absolute_url %}"><i class="fa fa-user"></i> Profile</a>
</li>
<li>
<a href="{% url 'edit_profile' %}"><i class="fa fa-edit"></i> Edit Profile</a>
</li>
<li>
<a href="{% url 'inbox' %}"><i class="fa fa-envelope-o"></i> Inbox</a>
</li>
<li>
<a href="#"><i class="fa fa-sign-out"></i> Logout</a>
</li>
</ul>
</nav>
任何人都有一些想法,因为我收到了错误?
在所有事情发生之前,我很感激你的关注。
答案 0 :(得分:2)
当您致电users
时,您需要包含名称空间reverse
。
return reverse('users:profile_agent', kwargs={'slug': self.slug})
在模板中,使用get_absolute_url
时无需使用网址标记。
{{ agent.get_absolute_url }}
在网址格式中,您应该转义连字符[\-\w]+
,或将其移至末尾[\w-]+
。