【问题标题】:How to insert different different email mysql in django如何在 django 中插入不同的电子邮件 mysql
【发布时间】:2021-04-21 08:04:14
【问题描述】:
I am new in django .. i have to created three id in that userid same but emailid should be diffrent i want i am sending three emaild it showing me one email id 3 times i am not understanding to store different different email id in id 1, id 2, id 3, but user name is same please help me how to save i amnot understanding in view.py i am add for loop relation database but its taking same mail id please help me how to do i amnot undestanding please................

我是 django 的新手 .. 我必须在该用户 ID 中创建三个相同的 ID,但电子邮件 ID 应该不同在 id 1、id 2、id 3 中,但用户名相同请帮助我如何保存我在 view.py 中不理解我添加了循环关系数据库但它采用相同的邮件 id 请帮助我怎么做我不明白请。

# views.py
def trade_references(request):
    #print('trade_reference', request.session.get('email'))
    if request.method == 'POST' and request.session.has_key('email'):
        print('trade_reference',request.session.get('email'))
       # arg1 = MyCompanyData.objects.filter(email=request.session.get('email')).last()
        #print('dfagda',arg1)
        Company_name = request.POST['Company_name']
        Company_name1 = request.POST['Company_name1']
        Company_name2 = request.POST['Company_name2']
        Contact_name = request.POST['Contact_name']
        Contact_name1 = request.POST['Contact_name1']
        Contact_name2 = request.POST['Contact_name2']
        Contact_Email = request.POST['Contact_Email']
        Contact_Email1 = request.POST['Contact_Email1']
        Contact_Email2 = request.POST['Contact_Email2']
        Address = request.POST['Address']
        Address1 = request.POST['Address1']
        Address2 = request.POST['Address2']
        phone = request.POST['phone']
        phone1 = request.POST['phone1']
        phone2 = request.POST['phone2']
        Length_of_relation = request.POST['Length_of_relation']
        Length_of_relation1 = request.POST['Length_of_relation1']
        Length_of_relation2 = request.POST['Length_of_relation2']
        Credit_limit = request.POST['Credit_limit']
        Credit_limit1 = request.POST['Credit_limit1']
        Credit_limit2 = request.POST['Credit_limit2']
        Average_Month = request.POST['Average_Month']
        Average_Month1 = request.POST['Average_Month1']
        Average_Month2 = request.POST['Average_Month2']
        data = TradeInformation(Company_name = Company_name, Company_name1 = Company_name1, Company_name2 = Company_name2,
                                Contact_name = Contact_name, Contact_name1 = Contact_name1, Contact_name2 = Contact_name2,
                                Contact_Email = Contact_Email,Contact_Email1 = Contact_Email1, Contact_Email2 = Contact_Email2,
                                Address = Address, Address1 = Address1, Address2 = Address2, phone = phone, phone1 = phone1,
                                phone2 = phone2, Length_of_relation = Length_of_relation, Length_of_relation1 =Length_of_relation1,
                                Length_of_relation2 = Length_of_relation2, Credit_limit = Credit_limit, Credit_limit1 = Credit_limit1,
                                Credit_limit2 = Credit_limit2, Average_Month = Average_Month, Average_Month1= Average_Month1,
                                Average_Month2= Average_Month2)
        data.save()
        for i in range(0,3):
            email = request.session.get('email')
            Contact_Email = request.POST['Contact_Email']
            Contact_Email1 = request.POST['Contact_Email1']
            Contact_Email2 = request.POST['Contact_Email2']
            related = Relationtable(userId=email,ContactEmailID=Contact_Email[i])
            related.save()

        subject = 'Email - Testing'
        html_content = render_to_string('Email_id.html')
        text_content = strip_tags(html_content)
        msg = EmailMultiAlternatives(subject, text_content,settings.EMAIL_HOST_USER, to=[Contact_Email, Contact_Email1, Contact_Email2])
        msg.attach_alternative(html_content, "text/html")
        msg.send()
        return HttpResponse('Trade reference data store in Database')
    else:
        return render(request, 'trade_references.html')

enter image description here

【问题讨论】:

    标签: django django-views django-templates


    【解决方案1】:

    Contact_Email[i] 不起作用,这只是获取Contact_Email,它是一个字符串,并从中获取第 i 个元素。如果要获取第 i 个元素,请创建一个列表:

    contact_emails = [
        request.POST['Contact_Email']
      , request.POST['Contact_Email1']
      , request.POST['Contact_Email2']
      ]
    email = request.session.get('email')
    for em in contact_emails:
        related = Relationtable.objects.create(userId=email, ContactEmailID=em)

    话虽如此,视图看起来太长而且太混乱了。通常使用forms [Django-doc] 来处理数据。此外,建模看起来不太好。通常您使用ForeignKeys 来引用另一个模型记录。这保证了参照完整性:如果您稍后更改TradeInformations 之一的DContact_Email,则需要大量工作来更新所有ForeignKeys 并以正确的方式执行此操作方式。

    【讨论】:

      猜你喜欢
      • 2019-10-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-09
      • 1970-01-01
      • 1970-01-01
      • 2020-07-05
      • 2013-10-31
      相关资源
      最近更新 更多