Reputation:
I'm getting this error while using one of my django views. I have 2 models; Owner and Property. An owner can have many properties. The models are as below:
class Owner(models.Model):
first_name = models.CharField(max_length=100)
last_name = models.CharField(max_length=100)
trading_name = models.CharField(max_length=100)
phone = models.CharField(max_length=50)
email = models.EmailField(unique=True)
address = models.CharField(max_length=200)
def __str__(self):
return self.trading_name
class Property(models.Model):
STYLE_CHOICES = [('Apart','Apartment'),
('Bung','Bungalow'),
('Mans','Mansionnete'),
('Comm','Commercial'),
]
name = models.CharField(max_length=100)
owner = models.ForeignKey(Owner, on_delete=models.CASCADE)
style = models.CharField(max_length=100, choices=STYLE_CHOICES)
bedrooms = models.IntegerField()
bathrooms = models.IntegerField()
parking = models.BooleanField(default=True)
sqft = models.IntegerField()
address = models.CharField(max_length=200)
description = models.TextField(blank=True)
image = models.ImageField(upload_to='photos/%Y/%m/%d', blank=True)
def __str__(self):
return self.name
My views are:
def CreatePropertyView(request):
if request.method == "POST":
form = PropertyForm(request.POST)
print(request.POST)
if form.is_valid():
property = form.save(commit=False)
property.save()
return redirect(reverse('properties'))
return render(request, 'property_agency/property_form.html', {'form': PropertyForm(request.POST or None)})
def PropertyListView(request):
properties = Property.objects.all()
context = {
'properties': properties
}
return render(request, 'property_agency/property_list.html', context=context)
def PropertyDetailView(request, property_id, *args, **kwargs):
property = Property.objects.get(pk=property_id)
units = PropertyUnit.objects.filter(property=property)
context = {
'property': property,
'units': units
}
return render(request, 'property_agency/property_detail.html', context=context)
The PropertyListView with url 'properties' is working just fine. Also PropertyDetailView is working fine, however when i go to 'properties/create_property' it blows out. The stack trace highlights the line:
property = Property.objects.get(pk=property_id)
of my PropertyDetailView as the line with an issue. It started after messing with migrations. I have tried deleting the database and removing migrations but still. Any ideas how to fix this, thanks.
My url config:
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('pages.urls')),
path('listings/', include('listings.urls')),
path('accounts/', include('accounts.urls')),
path('contacts/', include('contacts.urls')),
path('owners/', agency_views.OwnersListView, name='owners'),
path('owners/create_owner/', CreateOwnerView.as_view(), name='create_owner'),
path('owners/<owner_id>/', agency_views.OwnerDetailView, name='owner_detail'),
path('properties/', agency_views.PropertyListView, name='properties'),
path('properties/<property_id>/', agency_views.PropertyDetailView, name='property_detail'),
path('properties/create_property/', agency_views.CreatePropertyView, name='create_property'),
path('properties/create_unit/', agency_views.CreateUnitView, name='create_unit'),
path('tenants/', TenantListView.as_view(), name='tenants'),
path('tenants/create_tenant/', CreateTenantView.as_view(), name='create_tenant'),
path('realtors/', RealtorsListView.as_view(), name='realtors'),
path('realtors/create_realtor/', RealtorCreateView.as_view(), name='create_realtor'),
]
and in my html template:
<button type=""><a href="{% url 'create_property' %}">Create New Property</a></button>
<td><a href="{% url 'property_detail' property.id %}"><span class="badge badge-primary badge-pill">View</span></a></td>
Stacktrace:
Traceback (most recent call last):
File "/home/muhurijson/.local/lib/python3.8/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/home/muhurijson/.local/lib/python3.8/site-packages/django/core/handlers/base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/home/muhurijson/.local/lib/python3.8/site-packages/django/core/handlers/base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/muhurijson/Desktop/mySuite/projects/grant_ventures/property_agency/views.py", line 57, in PropertyDetailView
property = Property.objects.get(pk=property_id)
File "/home/muhurijson/.local/lib/python3.8/site-packages/django/db/models/manager.py", line 82, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/home/muhurijson/.local/lib/python3.8/site-packages/django/db/models/query.py", line 399, in get
clone = self.filter(*args, **kwargs)
File "/home/muhurijson/.local/lib/python3.8/site-packages/django/db/models/query.py", line 892, in filter
return self._filter_or_exclude(False, *args, **kwargs)
File "/home/muhurijson/.local/lib/python3.8/site-packages/django/db/models/query.py", line 910, in _filter_or_exclude
clone.query.add_q(Q(*args, **kwargs))
File "/home/muhurijson/.local/lib/python3.8/site-packages/django/db/models/sql/query.py", line 1290, in add_q
clause, _ = self._add_q(q_object, self.used_aliases)
File "/home/muhurijson/.local/lib/python3.8/site-packages/django/db/models/sql/query.py", line 1315, in _add_q
child_clause, needed_inner = self.build_filter(
File "/home/muhurijson/.local/lib/python3.8/site-packages/django/db/models/sql/query.py", line 1251, in build_filter
condition = self.build_lookup(lookups, col, value)
File "/home/muhurijson/.local/lib/python3.8/site-packages/django/db/models/sql/query.py", line 1116, in build_lookup
lookup = lookup_class(lhs, rhs)
File "/home/muhurijson/.local/lib/python3.8/site-packages/django/db/models/lookups.py", line 20, in __init__
self.rhs = self.get_prep_lookup()
File "/home/muhurijson/.local/lib/python3.8/site-packages/django/db/models/lookups.py", line 70, in get_prep_lookup
return self.lhs.output_field.get_prep_value(self.rhs)
File "/home/muhurijson/.local/lib/python3.8/site-packages/django/db/models/fields/__init__.py", line 966, in get_prep_value
return int(value)
ValueError: invalid literal for int() with base 10: 'create_property'
Upvotes: 0
Views: 46
Reputation: 10462
Use
path('properties/<int:property_id>/', agency_views.PropertyDetailView, name='property_detail'),
So that paths that look like properties/abc/
don't get routed to your PropertyDetailView
.
In general, you should always list the paths with angle brackets after similar paths without angle brackets, because Django picks the first matching routing rule.
Upvotes: 1
Reputation: 3717
I suppose you make a call to URL ... properties/create_property/ and it gets caught by the line above and 'create_property' is used as <property_id>
I think you should declare it as int:
../<int:property_id>/
path('properties/<property_id>/', agency_views.PropertyDetailView, name='property_detail'),
path('properties/create_property/', agency_views.CreatePropertyView, name='create_property'),
Upvotes: 0