如何在应用程序之间共享模型?

时间:2016-05-16 16:35:02

标签: python django django-forms

我的应用程序模型包含三个字段。第一个将与邮件应用程序共享作为邮件目的地;第二个将与aps应用程序共享,因为感谢页面index.html显示图表的频率。 我觉得我很接近但是当我在终端上写命令" runserver"它显示以下错误:

  

"需要更新。" %name django.core.exceptions.ImproperlyConfigured:   创建没有'字段的ModelForm'属性或   '排除'属性被禁止;表单ConfigurationFrorm需要   更新

这是我的代码,任何想法我做错了什么?

项目/喜爱将/ views.py

from django.shortcuts import render
from choix.forms import ConfigurationForm
from django.http import HttpResponse, HttpResponseRedirect
from django.core.urlresolvers import reverse
from choix.models import Configuration 
from django import forms 
class Meta:
        model = Configuration
def index(request):
    if request.method == 'GET' :
        form = ConfigurationForm()
        args = {}
        args.update(csrf(request))
        args['form'] = form 

        return render_to_response('create_article.html', args)
    else:
        form = ConfigurationForm(request.POST)
        if form.is_valid():
            e_mail = form.e_mail.data['e_mail']
            temps = form.temps.data['temps']
            temperature = form.temperature.data['temperature']
            post = m.Post.objects.create(e_mail=e_mail,
                                                         temps=temps, temperature = temperature)
            post.save()
            return HttpResponseRedirect(reverse('post_detail', kwargs={'post_id' : post.id}))


    return render(request, 'choix/configuration.html',  {'form': form})

项目/ APS / views.py

from django.shortcuts import render
from rasp import foo
from choix import views 
from choix.forms import ConfigurationForm
from django import forms
from choix.models import Configuration 

import json 


class ConfigurationFrorm(forms.ModelForm):
class Meta:
        model = Configuration
def index(request,self):
    cleaned_data = super(ConfigurationForm, self).clean()
    temps = cleaned_data.get("temps")

    return render(request, 'index.html', {'t' : foo(), 'form':form, 'f':temps})

项目/邮件/ views.py

from django.core.mail import send_mail, BadHeaderError
from django.http import HttpResponse, HttpResponseRedirect
from choix import views 
from choix.forms import ConfigurationForm
from django import forms 
from choix.models import Configuration 

class ConfigurationFrorm(forms.ModelForm):
class Meta:
        model = Configuration
def index(request,self):

    subject = request.POST.get('subject', 'subject')
    message = request.POST.get('message', 'attention ! la temperature a depasse le maximum ')
    from_email = request.POST.get('from_email', 'jkhgyfydfth@gmail.com')
    cleaned_data = super(ConfigurationForm, self).clean()
    to  = cleaned_data.get("email")
    if subject and message and from_email:
        try:
            send_mail(subject, message, from_email, [ to ])
            return HttpResponse('templates/mail.html')
        except BadHeaderError:
        return HttpResponse('Invalid header found.')
        return HttpResponseRedirect('mail')
    else:
        return HttpResponse('Make sure all fields are entered and valid.')

1 个答案:

答案 0 :(得分:1)

正如错误消息所示,从Django 1.8开始,您需要使用ConfigurationFormfields类属性明确告诉exclude您要包含哪些模型字段。如果您想要所有字段,只需说fields = '__all__'即可。 Django不再自动假设。

您发布的代码中存在多个缩进和拼写错误,这使得很难遵循。避免这种情况的最佳方法是复制并粘贴您的实际代码,而不是输入。