Django中的内联自定义表单

时间:2015-11-08 18:01:40

标签: django forms views inline models

我正在尝试创建一个系统来将应用程序(在Candidatura中名为models.py)添加到数据库中,并且在每个应用程序中,用户可以添加尽可能多的体验(名为CandidaturaExp在他们希望的models.py)中(一对多关系)。

我已经有forms.pyviews.py为应用程序工作,但我不知道如何操作,以便用户可以插入经验。我也不知道如何在我的模板中创建一个“PLUS”按钮来添加体验表单,因为我不是Java Script的专家。

所以,我有以下内容:

models.py

class Candidatura(models.Model):
    ## IDENTIFICACAO E  CONTACTO
    nome=models.CharField(max_length=200, verbose_name='Nome Completo')
    nacionalidade=models.CharField(max_length=200, verbose_name='Nacionalidade')
    data_nascimento=models.DateField(verbose_name='Data de Nascimento')
    residencia=models.CharField(max_length=200, verbose_name='Residência')
    localidade=models.CharField(max_length=200, verbose_name='Localidade')
    cod_postal=models.CharField(max_length=10, verbose_name='Código Postal')
    email=models.EmailField(max_length=254, verbose_name='Email')
    telefone=models.IntegerField(verbose_name='Telefone')

    ## HABILITACAO E CONHECIMENTOS
    nivel_academico=models.CharField(max_length=200, verbose_name='Nível Académico')
    ano_conclusao=models.CharField(max_length=4, verbose_name='Ano de Conclusão')
    curso=models.CharField(max_length=200, verbose_name='Curso')
    instituicao=models.CharField(max_length=200, verbose_name='Instituição')
    outras_hab=models.CharField(max_length=255, null=True, blank=True, verbose_name='Outras Habilitações')
    conhec_tec=models.CharField(max_length=255, null=True, blank=True, verbose_name='Conhecimentos Técnicos')

    ## DOCUMENTOS ANEXOS
    cv=models.FileField(upload_to=None, max_length=100, verbose_name='Curriculum Vitae', null=True, blank=True) 
    data_criado=models.DateTimeField(default=timezone.now, editable=False, verbose_name='Data de criação')

    def __unicode__(self):
        return self.nome

    ## EXPERIENCIA PROFISSIONAL
class CandidaturaExp(models.Model):
    candidatura = models.ForeignKey(Candidatura)    
    experiencia=models.BooleanField(verbose_name='Experiência Profissional')
    ano_admissao=models.CharField(max_length=4, null=True, blank=True, verbose_name='Ano de Admissão')
    ano_saida=models.CharField(max_length=4,null=True, blank=True, verbose_name='Ano de Saída')
    empresa=models.CharField(null=True, blank=True, max_length=200, verbose_name='Empresa')
    funcao=models.CharField(null=True, blank=True, max_length=255, verbose_name='Função Desempenhada')

    def __unicode__(self):
        return self.candidatura

class CandidaturaForm(ModelForm):
    class Meta:
        model = Candidatura
        fields = '__all__'

class CandidaturaExpForm(ModelForm):
    class Meta:
        model = CandidaturaExp
        fields = '__all__'  

forms.py

class NovaCandidatura(forms.ModelForm):
    class Meta:
        model = Candidatura
        fields = ('__all__')

class NovaCandidaturaExp(forms.ModelForm):  
    class Meta:
        model = CandidaturaExp
        fields = '__all__'

views.py

def inicio(request):
    return render_to_response('indexDB.html', locals(), context_instance=RequestContext(request))

@login_required    
def listing(request):
    table = CandidaturaTable(Candidatura.objects.all())
    return render(request, "listDB.html", {'table': table})

@login_required
def newform(request, template_name="NovaCandidatura.html"):
    if request.method == 'POST':
        form = NovaCandidatura(request.POST, request.FILES)
        if form.is_valid():
            form.save()
            url = urlresolvers.reverse('change_success')
            return HttpResponseRedirect(url)
    else:
        form = NovaCandidatura()
    page_title = ('Update user data')
    return render_to_response(template_name, locals(),
        context_instance=RequestContext(request))  

0 个答案:

没有答案