Django:在基于类的视图和表单中访问URL变量

时间:2013-03-19 22:43:06

标签: django django-views

我尝试使用基于标准类的视图和表单库在Django中创建对象。我的表单中的三个字段取决于从URL模式捕获的域变量。我的问题是:

  • 如何在domain中访问CreateSubscription,以便将Subscription.site设置为Site.objects.filter(domain=domain)[0]
  • 如何限制从CreateSubscriptionForm呈现的下拉字段,以便plan仅显示SubscriptionPlan.objects.filter(site=Site.objects.filter(domain=domain)[0])payment_profile仅限PaymentProfile.objects.filter(user=request.user)

为清楚起见,r'^subscribe/(?P<domain>\w+\.\w+)/$'中的域名与我自己的网站域名无关。 django.contrib.sites框架在这里没有帮助我。

提前感谢您帮助我解开所有这些CBV方法。 :)

网址格式为:

from django.conf.urls import patterns, url
from accounts.views import *

url(r'^subscribe/(?P<domain>\w+\.\w+)/$', 
    CreateSubscription.as_view(), name='subscribe_to_site'),
)

有问题的观点是:

from accounts.forms import *
from accounts.models import *

class CreateSubscription(CreateView):
    model = Subscription
    form_class = CreateSubscriptionForm

    def form_valid(self, form):
        form.instance.user = self.request.user
        return super(CreateSubscription, self).form_valid(form)

相关模型是:

from django.conf import settings
from django.contrib.sites.models import Site
from django.db import models

class PaymentProfile(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL)

class SubscriptionPlan(models.Model):
    site = models.ForeignKey(Site)
    name = models.CharField(max_length=40)

class Subscription(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL)
    site = models.ForeignKey(Site)
    plan = models.ForeignKey(SubscriptionPlan)
    payment_profile = models.ForeignKey(PaymentProfile)

最后,我的表格是:

from accounts.models import *

class CreateSubscriptionForm(forms.ModelForm):
    class Meta:
        model = Subscription
        exclude = ('user', 'site', )

1 个答案:

答案 0 :(得分:2)

要访问传递给视图的数据,请使用self.argsself.kwargs

from accounts.forms import *
from accounts.models import *

class CreateSubscription(CreateView):
    model = Subscription
    form_class = CreateSubscriptionForm

    def form_valid(self, form):
        form.instance.user = self.request.user
        form.instance.site = Site.objects.get(domain=self.kwargs['domain'])
        return super(CreateSubscription, self).form_valid(form)

要限制下拉内容,您需要为这些字段设置queryset。非常基本上,这些方面的东西:

class CreateSubscriptionForm(forms.ModelForm):
    plan = forms.ModelChoiceField(
        queryset=SubscriptionPlan.objects.filter(xxx)
    )

    ...

    class Meta:
        model = Subscription
        exclude = ('user', 'site', )

这也可以在视图中完成,以便您可以访问域。

class CreateSubscription(CreateView):
    model = Subscription
    form_class = CreateSubscriptionForm

    def get_form(self, form_class):
        """
        Returns an instance of the form to be used in this view.
        """
        form = super(CreateSubscription, self).get_form(form_class)
        form.fields['plan'].queryset = SubscriptionPlan.objects.filter(site__domain=self.kwargs['domain'])
        return form

    ...

    def form_valid(self, form):
        form.instance.user = self.request.user
        form.instance.site = Site.objects.get(domain=self.kwargs['domain'])
        return super(CreateSubscription, self).form_valid(form)