python - 从 URL 获取参数 slugs

标签 python django

我正在尝试从 URL 中获取两个参数以添加到我的上下文中。

这是网址:

  url(r'^company/(?P<company>[\w\-\_]+)/?/(?P<program>[\w\-\_]+)/?$', RegistrationView.as_view(),
                       name='test'), 

View :

class RegistrationView(RegistrationMixin, BaseCreateView):
    form_class = AppUserIntroducerCreateForm
    template_name = "registration/register_introducer.html"
    slug_field = 'company'



    def get_context_data(self, *args, **kwargs):
        context = super(RegistrationIntroducerView, self).get_context_data(**kwargs)
        print(self.get_slug_field())
        context['company'] = ??????
        context['program'] = ??????
        return context

我已经尝试了所有方法来获取值 self.companykwargs['company'] 等,我在这里做错了什么?

最佳答案

Here对你来说是引用。

context = super(RegistrationView, self).get_context_data(**kwargs)
print(self.get_slug_field())
context['company'] = self.kwargs['company']
context['program'] = self.kwargs['program']

关于python - 从 URL 获取参数 slugs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22782587/

相关文章:

python - Django runserver 日志输出到一个文件中

python - 查找 HDF5 数据集中的唯一列

python - 像素坐标与绘图坐标

python - Django 1.7 : Passing around unsaved instances throws "unhashable" exception

python - 如何使用 Django 模型字段定义保持 DRY

regex - Django URLS,使用 ?在网址中

python - 从多列python中查找

python - 你能用 pandas 对特定的字符间隔进行 value_counts 吗?

需要一些概述的 Python 代码

python - 从 Amazon S3 迁移到 Azure 存储(Django Web 应用程序)