i try to write code for editing records and its unit test. here is my code: test_view.py
def test_edit_address(self):
address1 = Address.objects.create(first_name='Edith', last_name='Star', address='Some City', phone_number='123455')
response = self.client.post(
reverse('edit', kwargs={'address_id': address1.id}),
{'first_name': 'Patrick', 'last_name': 'Stars', 'address': 'Other City', 'phone_number': '0123455'})
self.assertEqual(response.status_code, 302)
address1.refresh_from_db()
self.assertEqual(address1.first_name, 'Patrick')
views.py
from django.http import HttpResponseRedirect, HttpResponse
from django.shortcuts import redirect, render
from addressbooksapp.models import Address
from django.contrib import messages
def edit(request, address_id):
if request.method == 'POST':
addresses = Address.objects.get(pk=address_id)
addresses.first_name = request.POST.get('firstname_text')
addresses.last_name = request.POST.get('lastname_text')
addresses.address = request.POST.get('address_text')
addresses.phone_number = request.POST.get('phone_text')
addresses.save()
messages.success(request, ('Record Has Been Edited!'))
return redirect('home')
else:
addresses = Address.objects.get(pk=address_id)
return render(request, 'edit.html', {'addresses': addresses})
models.py
from django.db import models
class Address(models.Model):
first_name = models.CharField(max_length=200)
last_name = models.CharField(max_length=200)
address = models.TextField()
phone_number = models.CharField(max_length=200)
urls.py
from django.urls import path
from addressbooksapp import views
urlpatterns = [
path('', views.home_page, name='home'),
path('delete/<address_id>',views.delete, name='delete'),
path('edit/<address_id>',views.edit, name='edit'),
]
But when run the test i got error "django.db.utils.IntegrityError: NOT NULL constraint failed: addressbooksapp_address.first_name"
There is a mismatch between the data you send in the test, and you process in the view. In the test, you send a request with first_name
, last_name
, address
, and phone_number
as keys. But in the view you process data with firstname_text
, lastname_text
, address_text
and phone_text
.
You can alter the names, for example in the view with:
def edit(request, address_id):
if request.method == 'POST':
addresses = Address.objects.get(pk=address_id)
addresses.first_name = request.
addresses.last_name = request.
addresses.address = request.
addresses.phone_number = request.
addresses.save()
messages.success(request, ('Record Has Been Edited!'))
return redirect('home')
else:
addresses = Address.objects.get(pk=address_id)
return render(request, 'edit.html', {'addresses': addresses})
I would advice to here use subscripting, so POST[key]
over using the .get()
method, since then it will raise an error in case the key is missing.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.