如何使用CreateView的get_absolute_url

时间:2018-03-18 10:13:12

标签: django url

我希望用户能够在创建视图(create.html)中立即(不按下保存按钮)返回列表视图(index.html)。但我不确定如何指定实现此目的的链接。

我的create.html如下:

{% extends 'base.html' %}
{% block content %}
<h2>Create new system? <small><a href="{{ object.get_absolute_url }}">Go to system list?</a></small></h2>

{% include 'snippets/form-snippet.html' with form=form %}

{% endblock %}

系统url conf指定为:

app_name = 'system'
urlpatterns = [
    path('<int:pk_system>/', views.SystemUpdateView.as_view(), name='detail'),
    path('', views.SystemListView.as_view(), name='list'),
    path('create/', views.SystemCreateView.as_view(model=System), name='create')
]

我的SystemCreateView如下:

class SystemCreateView(LoginRequiredMixin, CreateView):
    template_name = 'system/create.html'
    form_class = SystemForm

    def form_valid(self, form):
        obj = form.save(commit=False)
        project_id = self.kwargs['pk']
        project = get_object_or_404(Project, pk=project_id)
        obj.project = project
        return super(SystemCreateView, self).form_valid(form)

    def get_form_kwargs(self):
        kwargs = super(SystemCreateView, self).get_form_kwargs()
        project_id = self.kwargs['pk']
        return kwargs

    def get_queryset(self):
        return System.objects.all() #filter(user=self.request.user)

    def get_context_data(self, *args, **kwargs):
        context = super(SystemCreateView, self).get_context_data(*args, **kwargs)
        context['title'] = 'Create System'
        return context

System model和get_absolute_url方法指定如下:

class System(models.Model):
    project     = models.ForeignKey('solgeo.Project', related_name='systems', on_delete=models.CASCADE)
    system_name = models.CharField(max_length=200)


    def get_absolute_url(self):
        return reverse('solgeo:system:detail', kwargs={'pk': self.pk, 'pk_system':self.project.id})

2 个答案:

答案 0 :(得分:0)

好的找到了以下解决方案:

class SystemCreateView(LoginRequiredMixin, CreateView):
    template_name = 'system/create.html'
    form_class = SystemForm

    def form_valid(self, form):
        obj = form.save(commit=False)
        project_id = self.kwargs['pk']
        project = get_object_or_404(Project, pk=project_id)
        obj.project = project
        return super(SystemCreateView, self).form_valid(form)

    def get_form_kwargs(self):
        kwargs = super(SystemCreateView, self).get_form_kwargs()
        project_id = self.kwargs['pk']
        project = get_object_or_404(Project, pk=project_id)
        kwargs['project_id'] = project.id
        return kwargs

    def get_queryset(self):
        return System.objects.all()

    def get_context_data(self, *args, **kwargs):
        context = super(SystemCreateView, self).get_context_data(*args, **kwargs)
        project_id = self.kwargs['pk']
        context['title'] = 'Create System'
        context['project_id'] = project_id  # add project_id to context
        return context

现在我可以在我的create.html模板中访问上下文变量project_id,即:

{% extends 'base.html' %}
{% block content %}
<h2>Create new system? <small><a href="{% url 'solgeo:system:list' project_id %}">Go to system list ?</a></small></h2>

{% include 'snippets/form-snippet.html' with form=form %}

{% endblock %}

答案 1 :(得分:0)

假设您没有其他名称空间链接到此网址文件

path('system/', include('system.urls')),

 {%  url 'list' %} #the url to listview

但如果你的主网址链接到这个文件就像那样

path('system/', include('system.urls', namespace='system')),

  {%  url 'system:list' %} #namespace go first : url name