我想将表单数据提交到电子邮件模板,然后发送所述电子邮件。但是,响应中没有返回任何数据。我相信这是一个简单的问题,但是我对Python / Django缺乏经验。我认为我有一些小错误,这会引起问题。非常感谢任何输入!
Views.py
def new_opportunity_submit(request):
form_class = OpportunityForm
if request.method == 'POST':
form = form_class(data=request.POST)
if form.is_valid():
account_manager = request.POST.get('account_manager')
inside_solutions = request.POST.get('inside_solutions')
selected_company = request.POST.get('selected_company')
# Email the profile with the
# contact information
template = get_template('new_opportunity_submit.txt')
context = {
'account_manager': account_manager,
'inside_solutions': inside_solutions,
'selected_company': selected_company
}
content = template.render(context)
email = EmailMessage(
"New contact form submission",
content,
"Your website" + '',
['youremail@gmail.com'],
headers={'Reply-To': ''}
)
email.send()
return redirect('opportunities')
return render(request, 'website/opportunities.html', {'form': form_class})
Opportunities.html
<form action="{% url 'new_opportunity_submit' %}" method="post" id="newOpportunityForm" data-location-url="{% url 'new_opportunity_location' %}"
data-contact-url="{% url 'new_opportunity_contact' %}" novalidate>
{% csrf_token %}
<div class="field">
<label class="label">Account Manager:</label>
<div class="select">
<select name="account_manager" id="account_manager">
<option value="">Select</option>
<option value="">Person 1</option>
<option value="">Person 2</option>
</select>
</div>
</div>
<div class="field">
<label class="label">Inside Solutions:</label>
<div class="select">
<select name="inside_solutions" id="inside_solutions">
<option value="">Select</option>
<option value="">Person 1</option>
<option value="">Person 2</option>
</select>
</div>
</div>
<div class="field">
<label class="label">Client Information:</label>
<div class="select">
<select name="selectcompanies" id="selectcompanies">
<option value="">Company</option>
{% for company in companies %}
<option value="" name="selected_company" id="selected_company">{{ company.name }}</option>}
{% endfor %}
</select>
</div>
Forms.py
from django import forms
from .models import Opportunity, ClientInformation, OpportunityName, DealRegistration
class OpportunityForm(forms.Form):
class Meta:
model = Opportunity
fields = ('account_manager', 'inside_solutions', 'selected_company')
Models.py
class CloseDate(models.Model):
date = models.DateField()
class DealRegistration(models.Model):
deal = models.CharField(max_length=1000)
def __str__(self):
return self.deal
class OpportunityName(models.Model):
company = models.CharField(max_length=30)
practice = models.CharField(max_length=30)
projectName = models.CharField(max_length=30)
def __str__(self):
return self.company
class ClientInformation(models.Model):
company = models.CharField(max_length=30)
location = models.CharField(max_length=30)
contact = models.CharField(max_length=30)
def __str__(self):
return self.company
class Opportunity(models.Model):
accountManger = models.CharField(max_length=30)
insideSolutions = models.CharField(max_length=30)
clientInformation = models.ForeignKey(ClientInformation, on_delete=models.SET_NULL, null=True)
opportunityName = models.ForeignKey(OpportunityName, on_delete=models.SET_NULL, null=True)
dealRegistration = models.CharField(max_length=30)
closeDate = models.CharField(max_length=30)
def __str__(self):
return self.opportunityName
Company.Object
[
{
"id": 0,
"identifier": "string",
"name": "string",
"status": {
"id": 0,
"name": "string",
"_info": {}
},
"addressLine1": "string",
"addressLine2": "string",
"city": "string",
"state": "string",
"zip": "string",
"country": {
"id": 0,
"identifier": "string",
"name": "string",
"_info": {}
},
"phoneNumber": "string",
"faxNumber": "string",
"website": "string",
"territory": {
"id": 0,
"name": "string",
"_info": {}
},
"market": {
"id": 0,
"name": "string",
"_info": {}
},
"accountNumber": "string",
"defaultContact": {
"id": 0,
"name": "string",
"_info": {}
},
"dateAcquired": "2019-06-10T16:08:25Z",
"sicCode": {
"id": 0,
"name": "string",
"_info": {}
},
"parentCompany": {
"id": 0,
"identifier": "string",
"name": "string",
"_info": {}
},
"annualRevenue": 0,
"numberOfEmployees": 0,
"ownershipType": {
"id": 0,
"name": "string",
"_info": {}
},
"timeZoneSetup": {
"id": 0,
"name": "string",
"_info": {}
},
"leadSource": "string",
"leadFlag": true,
"unsubscribeFlag": true,
"calendar": {
"id": 0,
"name": "string",
"_info": {}
},
"userDefinedField1": "string",
"userDefinedField2": "string",
"userDefinedField3": "string",
"userDefinedField4": "string",
"userDefinedField5": "string",
"userDefinedField6": "string",
"userDefinedField7": "string",
"userDefinedField8": "string",
"userDefinedField9": "string",
"userDefinedField10": "string",
"vendorIdentifier": "string",
"taxIdentifier": "string",
"taxCode": {
"id": 0,
"name": "string",
"_info": {}
},
"billingTerms": {
"id": 0,
"name": "string",
"_info": {}
},
"invoiceTemplate": {
"id": 0,
"name": "string",
"_info": {}
},
"pricingSchedule": {
"id": 0,
"name": "string",
"_info": {}
},
"companyEntityType": {
"id": 0,
"name": "string",
"_info": {}
},
"billToCompany": {
"id": 0,
"identifier": "string",
"name": "string",
"_info": {}
},
"billingSite": {
"id": 0,
"name": "string",
"_info": {}
},
"billingContact": {
"id": 0,
"name": "string",
"_info": {}
},
"invoiceDeliveryMethod": {
"id": 0,
"name": "string",
"_info": {}
},
"invoiceToEmailAddress": "string",
"invoiceCCEmailAddress": "string",
"deletedFlag": true,
"dateDeleted": "2019-06-10T16:08:25Z",
"deletedBy": "string",
"mobileGuid": {},
"facebookUrl": "string",
"twitterUrl": "string",
"linkedInUrl": "string",
"currency": {
"id": 0,
"symbol": "string",
"isoCode": "string",
"name": "string",
"_info": {}
},
"territoryManager": {
"id": 0,
"identifier": "string",
"name": "string",
"_info": {}
},
"typeIds": [
0
],
"_info": {},
"customFields": [
{
"id": 0,
"caption": "string",
"type": "Button",
"entryMethod": "EntryField",
"numberOfDecimals": 0,
"value": "string"
}
]
}
]
依赖下拉脚本
<script>
$("#selectcompanies").change(function () {
var location_url = $("#newOpportunityForm").attr("data-location-url");
var contact_url = $("#newOpportunityForm").attr("data-contact-url");
var optionSelected = $(this).find("option:selected");
var company = optionSelected.text();
$.ajax({ // initialize an AJAX request
url: location_url,
data: {
'selected_company': company
},
success: function (data) {
console.log(data)
$("#selectlocations").html(data);
$("#auto_company").html(company);
}
});
$.ajax({ // initialize an AJAX request
url: contact_url,
data: {
'selected_company': company
},
success: function (data) {
console.log(data)
$("#selectcontacts").html(data);
}
});
});
</script>
答案 0 :(得分:0)
您需要为<option>
DOM元素提供一个value
。现在他们是空白。尝试这样的事情:
<form action="{% url 'new_opportunity_submit' %}" method="post" id="newOpportunityForm" data-location-url="{% url 'new_opportunity_location' %}"
data-contact-url="{% url 'new_opportunity_contact' %}" novalidate>
{% csrf_token %}
<div class="field">
<label class="label">Account Manager:</label>
<div class="select">
<select name="account_manager" id="account_manager">
<option value="">Select</option>
<option value="person_1">Person 1</option>
<option value="person_2">Person 2</option>
</select>
</div>
</div>
此外,在您的views.py
中,一旦使用form = form_class(request.POST)
填充表单,您就可以执行以下操作:
account_manager = form_class.cleaned_data['account_manager']
如果您在表单中使用的字段与模型上的字段相同,则可能需要考虑使用ModelForm。它们更加清洁和易于维护。如果您使用模型表单,则可以在视图中执行以下操作:
form = OpportunityForm(request.POST)
form.save()