'str'对象不支持项目分配 - django 错误

发布于 2024-10-28 23:22:30 字数 3049 浏览 6 评论 0原文

我创建了一个 Django 应用程序。该应用程序有一个用户注册页面。注册页面已实现,我试图在注册后实现电子邮件确认。即当用户注册时发送电子邮件。在执行此操作时,我遇到了此错误,“‘str’对象不支持项目分配”。有人可以帮我解决这个问题吗?将在这里粘贴我的代码。

def registrationForm(request):
    if request.method == "POST":  
        firstName = request.POST.get("firstName")
        lastName = request.POST.get("lastName")
        email = request.POST.get("email")
        password = request.POST.get("password")
        sex = request.POST.get("sex")
        birthday = request.POST.get("birthday")
        print request.POST.get("sex")
        UniversityDetails(firstName=firstName,lastName=lastName,email=email,password=password,sex=sex,birthday=birthday).save()

        msg = "Registration Successfull"
        msg['Subject'] = 'The contents of %s' 
        msg['From'] = me
        msg['To'] =you 
        s = smtplib.SMTP()
        s.sendmail(me, [you], msg.as_string())
        s.quit()

        return render_to_response('registrationForm.html')

    return render_to_response("registrationForm.html")

html

<form name ="myform"  method="POST" id='FormID'>

<table>
<tr>
<td>First name</td>
<td>
<input type="text" name="firstName" value=""  maxlength="100" />
<b id="firstNameID" style="font-family:Times New Roman;color:#B4045F;font-size:14px;">
</td>
</tr>

<tr>
<td>Last name</td>
<td>
<input type="text" name="lastName" value="" maxlength="100" />
</td>
</tr>

<tr>
<td>E-mail</td>
<td>
<input type="text" name="email" value="" maxlength="100" />
</td>
</tr>

<tr>
<td>Password</td>
<td>
<input type="password" name="password" value="" maxlength="100"  />
<b id="passwordID" style="font-family:Times New Roman;color:#B4045F;font-size:14px;">
</td>
</tr>

<tr>
<td>Gender:</td>
<td>
<input type="radio" name="sex" value="male" /> Male
<input type="radio" name="sex" value="female" /> Female
</td>
</tr>

<tr>
<td>Birthday</td>
<td>
<input type="text" name="birthday" id='datepicker' value="" maxlength="100" />
</td>
</tr>


</tr>
</table>


<script type="text/javascript">
function isEmpty(){
    if  ((document.myform.firstName.value.length==0)) 
        {
        document.getElementById('firstNameID').innerHTML = 'Please fill this field';
        return true;
        }
    else if ((document.myform.password.value.length==0)) 
        {
        document.getElementById('passwordID').innerHTML = 'Please fill this field';
        return true;
        }
    else if (! ValidCaptcha())
        {txtCaptcha
            alert("Captcha entered wrong");
        }

    else 
        { 
        document.getElementById('FormID').action = "http://10.1.0.90:8080/registrationForm/";
        document.getElementById('FormID').submit();
        return false; 
        }
}
</script>

I created a Django app. The application has a user regstration page. The registration page is implemented and i was trying to implement a email confirmation after registering. ie sending an email when the user is registered. while doing this i got into this error," 'str' object does not support item assignment ". Can somebody help me to solve this. will paste my code here.

def registrationForm(request):
    if request.method == "POST":  
        firstName = request.POST.get("firstName")
        lastName = request.POST.get("lastName")
        email = request.POST.get("email")
        password = request.POST.get("password")
        sex = request.POST.get("sex")
        birthday = request.POST.get("birthday")
        print request.POST.get("sex")
        UniversityDetails(firstName=firstName,lastName=lastName,email=email,password=password,sex=sex,birthday=birthday).save()

        msg = "Registration Successfull"
        msg['Subject'] = 'The contents of %s' 
        msg['From'] = me
        msg['To'] =you 
        s = smtplib.SMTP()
        s.sendmail(me, [you], msg.as_string())
        s.quit()

        return render_to_response('registrationForm.html')

    return render_to_response("registrationForm.html")

html

<form name ="myform"  method="POST" id='FormID'>

<table>
<tr>
<td>First name</td>
<td>
<input type="text" name="firstName" value=""  maxlength="100" />
<b id="firstNameID" style="font-family:Times New Roman;color:#B4045F;font-size:14px;">
</td>
</tr>

<tr>
<td>Last name</td>
<td>
<input type="text" name="lastName" value="" maxlength="100" />
</td>
</tr>

<tr>
<td>E-mail</td>
<td>
<input type="text" name="email" value="" maxlength="100" />
</td>
</tr>

<tr>
<td>Password</td>
<td>
<input type="password" name="password" value="" maxlength="100"  />
<b id="passwordID" style="font-family:Times New Roman;color:#B4045F;font-size:14px;">
</td>
</tr>

<tr>
<td>Gender:</td>
<td>
<input type="radio" name="sex" value="male" /> Male
<input type="radio" name="sex" value="female" /> Female
</td>
</tr>

<tr>
<td>Birthday</td>
<td>
<input type="text" name="birthday" id='datepicker' value="" maxlength="100" />
</td>
</tr>


</tr>
</table>


<script type="text/javascript">
function isEmpty(){
    if  ((document.myform.firstName.value.length==0)) 
        {
        document.getElementById('firstNameID').innerHTML = 'Please fill this field';
        return true;
        }
    else if ((document.myform.password.value.length==0)) 
        {
        document.getElementById('passwordID').innerHTML = 'Please fill this field';
        return true;
        }
    else if (! ValidCaptcha())
        {txtCaptcha
            alert("Captcha entered wrong");
        }

    else 
        { 
        document.getElementById('FormID').action = "http://10.1.0.90:8080/registrationForm/";
        document.getElementById('FormID').submit();
        return false; 
        }
}
</script>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

撞了怀 2024-11-04 23:22:30

问题就在这里:

msg = "Registration Successfull"
msg['Subject'] = 'The contents of %s'

string是不可变的,并且不像dict那样。您还应该使用 Django Forms,而不是自己完成这一切。

Django 还内置了对发送电子邮件的支持。阅读发送电子邮件文档以了解如何使用它。

The problem is here:

msg = "Registration Successfull"
msg['Subject'] = 'The contents of %s'

strings are immutable and are not dict-like. You should also be using Django Forms, rather than doing it all yourself.

Django also has built-in support for sending emails. Read the Sending E-mail documentation to see how to use it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文