我正在开发一个简单的社交网络应用程序,您可以在墙上发布该应用程序。尽管在存储日期时间上采取了许多不同的方法,但我仍然收到以下错误。但是,对于其他应用程序上的相同数据类型,一切正常。我已经坚持了两天了。
TypeError at /wallpost/add_wallpost/
expected string or bytes-like object
Request Method: POST
Request URL: http://localhost:8000/wallpost/add_wallpost/
Django Version: 2.0.2
Exception Type: TypeError
Exception Value:
expected string or bytes-like object
Exception Location: C:\Users\Rhonald.Rose\AppData\Local\Continuum\anaconda3\envs\mydjangoenv\lib\site-packages\django\utils\dateparse.py in parse_datetime, line 107
Python Executable: C:\Users\Rhonald.Rose\AppData\Local\Continuum\anaconda3\envs\mydjangoenv\python.exe
Python Version: 3.6.5
我的模型。py
class Wallpost(models.Model):
content = models.CharField(max_length=1000, blank=True, null=True)
author = models.ForeignKey('members.MemberInfo', blank=True, null=True, related_name='postautor_member_set', on_delete=models.CASCADE)
parent = models.ForeignKey('wallpost.Wallpost', blank=True, null=True, related_name='parent_wallpost_set', on_delete=models.CASCADE)
ref_number = models.IntegerField(default=0)
module_ref = models.IntegerField(default=0)#General (wall), Responses (wall), Likes, Blogs, Reshare
category = models.IntegerField(default=0)#Information, Approval or Action, Alert, Warning, Danger
button_type = models.IntegerField(default=0)#Social, Approval, Acceptance, Information, Alert, Warning
privacy = models.IntegerField(default=0)#All, OnlySignedIn, OnlyFollowers, OnlyMutualFollowers, ExplicitIncluded, OnlyGroupMembers, Reported
#date_created = models.DateField(blank=False,auto_now_add=True)
date_created = models.DateTimeField(default=timezone.now())
is_archived = models.BooleanField(default=False,blank=False, null=False)
allow_reshare = models.BooleanField(default=False,blank=False, null=False)
date_reported = models.DateTimeField(default=False,blank=True,null=True)
我的模板(HTML):
<div class="form-group">
<div class="card">
<div class="card-header">
Make a post
</div>
<div class="card-body">
<form method="POST" action="add_wallpost/">
{% csrf_token %}
<div class="row">
<div class="col-sm-8">
{{form.content}}
</div>
</div>
<br>
<div class="row">
<div class="col-sm-10">
<span class='sel1'>Privacy: </span>
{{form.privacy}}
</div>
<div class="col">
<input type="submit" class="btn btn-primary" name="addpost" value="Post">
</div>
</div>
</form>
</div>
</div>
</div>
我的urls.py
urlpatterns = [
url(r'^$',views.WallpostListView.as_view(), name='all'),
url(r'^add_wallpost/$',views.add_wallpost, name='add_wallpost'),
]
我的forms.py
class WallpostForm(forms.ModelForm):
priv_list = [
(1,"All"),
(2,"Only Signed In"),
(3,"Only Followers"),
(4,"Only Mutual Followers"),
(5,"Only Explicitly Included"),
]
privacy = forms.ChoiceField(choices=priv_list)
class Meta():
model = Wallpost
fields = ("content","privacy")
widgets = {
'content':forms.TextInput(attrs={'class':'form-control'}),
'privacy':forms.Select(attrs={'class':'form-control'}),
}
我的views.py
class WallpostListView(FormMixin, ListView):
model = WallpostRecipients
form_class = WallpostForm
template_name = 'wall.html'
def get_queryset(self):
return WallpostRecipients.objects.filter(
recipient_id = self.request.user.pk,
is_deleted = False
)
def add_wallpost(request):
if request.method == "POST":
print("inside add_wallpost with request.method == POST")
form = WallpostForm(request.POST)
if form.is_valid():
print ("form.is_valid succeeded!")
content = form.cleaned_data.get('content')
privacy = form.cleaned_data.get('privacy')
mrec = Wallpost()
mrec.content = content
#mrec.date_created = timezone.now
mrec.author = request.user
mrec.privacy = privacy
print (mrec.date_created)
try:
mrec.save()
print("saved successfully...")
except Exception as e:
print(e)
raise (e)
else:
for error in form.errors:
print(error)
return redirect('wallpost:all')