1👍
You are trying to create a Name
row without a messager_name
which is a required field by default when using CharField
. You need to make this field nullable:
massenger_name = models.CharField(max_length=50, null=True)
You need to makemigrations
and migrate
to apply your changes. This will allow you to have a Name
row without a messager_name
.
0👍
Please do not use .get(…)
[Django-doc], it silences an error when the key is missing, which is the case here.
You should check what method is used and in case of a POST request, handle it properly:
from django.shortcuts import redirect
from .models import Name
# Create your views here.
def Home(request):
if request.method == 'POST':
Name.objects.create(massenger_name=request.POST['user_name'])
return redirect(Home)
return render(request , 'index.html')
In the model, it makes more sense to use auto_now_add=True
[Django-doc] for the action_time
:
class Name(models.Model):
massenger_name = models.CharField(max_length=50)
action_time = models.DateTimeField(auto_now_add=True)
# …
Note: In case of a successful POST request, you should make a
redirect
[Django-doc]
to implement the Post/Redirect/Get pattern [wiki].
This avoids that you make the same POST request when the user refreshes the
browser.
Note: Functions are normally written in snake_case, not PascalCase, therefore it is
advisable to rename your function tohome
, not.Home
- [Answered ]-Django giving error when trying to get path to static file
- [Answered ]-Pulling data from uploaded text file into django database
- [Answered ]-Dajngo: Pagination in search results with Class-based views
- [Answered ]-Form action isn't triggering at all