django and url creation - django

I want to construct the following url
domain/edit/xray/<id>/<image_name>
where id is a model's id and image_name is the name of an image.
I have an app that will handle image editing with its own views lets say it mypil
mypil.views
def edit_xray(request, id, image_name):
....code to get image by name
....model by id and edit image using PIL
model = Model.objects.get(pk=id)
project/urls.py
url(r'^edit/', include(mypil.urls))
mypil.urls
url(r'^xray/(?P<id>)\d+/(?P<image_name>)\w+\.\w{3}$', mypil.views.edit_xray)
But when i try to view edit/xray/1/image.jpg (both id and image exist) i encounter this problem.
invalid literal for int() with base 10: ''
and the traceback shows me that line above
model=Model.objects.get(pk=id)
Does the empty string('') mean that it doesn't parse the id correctly from the URL? Isn't my url pattern correct?
EDIT: Damn my eyes....needed to put \d+ and the image reg pattern inside the parentheses
url(r'^xray/(?P<id>\d+)/(?P<image_name>\w+\.\w{3})$,...)
Sorry for posting a question...(How do i delete my own questions?)

The correct url is:
url(r'^xray/(?P<id>\d+)/(?P<image_name\w+\.\w{3})$', mypil.views.edit_xray)
You should put the pattern in the parentnesses.

Related

Django funcion views, creating a company with user associated

So, i have this problem when im trying to create my "company", when i didnt have user authentication the view worked just fine. Now i've added django allauth, and this screen appears to me:
enter image description here
So i will leave here my model and my view to create the company
Im just a begginner so sry for my bad explanation
enter image description here
enter image description here
The URLS : https://prnt.sc/K-lKvmfuQtvR
It looks like the problem is with the way your url is setup. Do you have a screenshot of the urls.py as well so we can see how you setup your url mapping. (OP added this screenshot: prnt.sc/K-lKvmfuQtvR)
Your companies/<pk>/ endpoint is catching your "create_company" value and storing it as a variable. Try moving your companies/create_company path above your companies/<pk>/ like so
path('companies/', listing_companies),
path('companies/create_company/', company_create),
path('companies/<pk>', retrieve_companies),
path('companies/<pk>/update/', company_update),
path('companies/<pk>/delete/', company_delete),
Alternatively rename companies/create_company/ to create_company/ like so
path('companies/', listing_companies),
path('companies/<pk>', retrieve_companies),
path('create_company/', company_create),
path('companies/<pk>/update/', company_update),
path('companies/<pk>/delete/', company_delete),
The end goal in either case is simply to make sure your paths are not capturing your "create_company" and storing it as that variable that it is passing along to your path's method.

how to create params in django url?

I have a django project where url is like this
url(r'^invoice/(?P<invoice_id>[A-Za-z0-9]+)/(?P<order_id>[A-Za-z0-9]+)$',GenerateInvoicePdf,name='invoice'),
which generates url localhost:8000/invoice/2341wq23fewfe1231/3242
but i want url to be like localhost:8000/invoice?invoice_id=2341wq23fewfe1231&order_id=3242
i tried documentation and used syntax like this re_path(r'^comments/(?:page-(?P<page_number>\d+)/)?$', comments), But did not get desired result.
how can i do so?
The parts which you are trying to write after ? is called url query string. You don't need to define them in the urls.py. You can just use:
re_path(r'^comments/$', comments),
And inside comments views, you can access the query string like this:
def comments(request):
invoice_id = request.GET.get('invoice_id')
order_id = request.GET.get('order_id')
# rest of the code

What would be Djnago's url pattern to match and fetch out a url (coming appended to site's domain as a GET request)?

Suppose my site's domain is mysite.com , now whenever a request comes in this form : mysite.com/https://stackoverflow.com :I want to fetch out this url "https://stackoverflow.com" and send it to the corresponding view.
I have tried this pattern :
url(r'^(?P<preurl>http[s]?://(?:[a-zA-Z]|[0-9]|[$-_#.&+]|[!*(),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+)$',prepend_view)
regex of which matches the incoming appended url and assigns variable preurl the value "https://stackoverflow.com", which I access in corresponding view function .
This works fine for above example but my url pattern is failing in case of some exceptional urls..
Please suggest a robust url pattern by taking into consideration all exceptional urls too, like the following:
ftp://ftp.is.co.za/rfc/rfc1808.txt
http://www.ietf.org/rfc/rfc2396.txt
ldap://[2001:db8::7]/c=GB?objectClass?one
mailto:John.Doe#example.com
news:comp.infosystems.www.servers.unix
tel:+1-816-555-1212
telnet://192.0.2.16:80/
urn:oasis:names:specification:docbook:dtd:xml:4.1.2
That is, if a request comes like :
mysite.com/ldap://[2001:db8::7]/c=GB?objectClass?one
I should be able to get the value "ldap://[2001:db8::7]/c=GB?objectClass?one" in variable preurl
You don't have to make this type of complex url pattern, First, make a URL pattern that matches everything.
url(r'^.*/$', views.fast_track_service, name='fast_track'),
and append it to the end in urlpatterns in your urls.py then in your view, Use request object, So You can get the full path of get request with this method,
fast_track_url = request.get_full_path()[1:]
and then once you got the url try validating that with URLValidator like this.
if not 'http://' in fast_track_url and not 'https://' in fast_track_url:
fast_track_url = 'http://' + fast_track_url
url_validate = URLValidator()
try:
url_validate(fast_track_url)
except:
raise Http404
If you want to validate other complicated URL like mailto etc, then you can write your own validator.

Send data from template to a view django

In my application data about a entry is displayed detailing their information. You can navigate between the entries via a hyperlink.
So far the code:
{{park.name}}
Has been suffient in dealing with this. The id is captured in the urls.py and onto views.py
The problem I now face is to deal with my 'location' entry. Examples of locations are 'Europe, UK', 'USA, New York'.
I know that:
{{park.location}}
with:
url(r'^location/(?P<park_location>\d+)$', Location_Main),
won't work, due to the spaces and commas etc.
How would I resolve this?
I would also like the 'location' view and 'location' url to handle the location of a parent company say ()
Thanks in advance
Why not pass pass park.id and then in view get the park object and then get its location:
the url:
url(r'^location/(?P<park_id>\d+)$', Location_Main, name="park_location"),
the template:
{{park.location}}
the view:
def Location_Main(request, park_id):
park = get_object_or_404(Park, pk=park_id)
location = park.location
Alternatively send the location as GET parameter:
the url:
url(r'^location/$', Location_Main, name="park_location"),
the template:
{{park.location}}
the view:
def Location_Main(request):
location = request.GET.get('location')
url(r'^location/(?P<park_location>[a-zA-Z0-9_.-]+)/$', Location_Main),
You will need to remove punctuation and non-english characters from the location names before using them in the url. Alternatively you can remove them when you define park.location.
You can use urlencode template filter to escape the characters as
{{park.location}}
With reference to this question you may have to change the url pattern as
url(r'^location/(?P<park_location>[\w|\W]+)$', Location_Main)

Django : Getting an instace of a model based on a static location

Lets say I have this model
class Egg(models.Model):
file = FileField(upload_to='media')
img = ImageField(upload_to='media')
How to get an Egg instance if I only have the file URL string like 'http://example.com/media/spam.tar.gz'? Can I query by this URL??
Which part of the url you want to query by? By spam.tar.gz? If so, you can try:
Egg.objects.filter(file__icontains='spam.tar.gz').