Reputation: 81
I am creating a web application where I need IP addresses to be a URL parameter in Django. However, I want to remove the dots as they aren't valid to use in a URL. Maybe replace them with dashes? Is there any good way of doing this?
Here is my model:
models.py
class Machine(models.Model):
ip = models.CharField(max_length=15, unique=True) #255.255.255.255
name = models.CharField(max_length=16, default="") #SSH-Server
number_of_threats = models.IntegerField(default=0)
Here is my view:
def machine(request, ip):
print machine_ip
ip = Machine.objects.get(ip=ip)
context_dict['machine_ip'] = machine_ip
return render(request, 'topology/machine.html', context_dict)
The format I have now is: /machine/192.168.1.1 The format I a want to have is: /machine/192-168-1-1 (or similar)
I looked at Slugify as it is does a similar function, but it doesn't look like it will do the same with dots.
Any suggestions on a good way of doing this?
Upvotes: 0
Views: 497
Reputation: 2990
def machine(request, ip):
print machine_ip
ip = Machine.objects.get(ip=ip)
context_dict['machine_ip'] = machine_ip.replace('.', '-')
return render(request, 'topology/machine.html', context_dict)
and in urls.py make parameter ip
(?P<ip>.*)
Upvotes: 2