how to request last segment of url in Flask - flask

I feel like the answer is simple, yet I can't seem to figure it out. I have a URL:
http://127.0.0.1:5000/fight_card/fight/5
And I'm trying to use just the "5" in my code as that is the ID for the current fight in the SQL table. So far, I've tried
fight = Fight.query.filter_by(id=request.path).first()
However that returns:
fight_card/fight/5
Is there any way I can use "request" to target just the 5? Thank you in advance!

You should include a variable, current_fight_id, in your route definition. You can then access that variable in your view function.
#app.route('/fight_card/fight/<current_fight_id>')
def fight_route(current_fight_id):
print(current_fight_id) # use variable in route
Alternatively, you could use the approach you're using but modify the string that's returned. If you have a string:
endpoint = "fight_card/fight/5" # returned by your current code
You can access the five (current_fight_id) with:
current_fight_id = endpoint.split("/")[-1] # grab the segment after the last "/"

request.path would give you: /fight_card/fight/5. Then, you can split('/') to get a list of the parts.

Related

How to use variable at start of django url to return to view?

I am trying to pass the first part of a django url to a view, so I can filter my results by the term in the url.
Looking at the documentation, it seems quite straightforward.
However, I have the following urls.py
url('<colcat>/collection/(?P<name>[\w\-]+)$', views.collection_detail, name='collection_detail'),
url('<colcat>/', views.collection_view, name='collection_view'),
In this case, I want to be able to go to /living and have living be passed to my view so that I can use it to filter by.
When trying this however, no matter what url I put it isn't being matched, and I get an error saying the address I put in could not be matched to any urls.
What am I missing?
<colcat> is not a valid regex. You need to use the same format as you have for name.
url('(?P<colcat>[\w\-]+)/collection/(?P<name>[\w\-]+)$', views.collection_detail, name='collection_detail'),
url('(?P<colcat>[\w\-]+)/$', views.collection_view, name='collection_view'),
Alternatively, use the new path form which will be much simpler:
path('<str:colcat>/collection/<str:name>', views.collection_detail, name='collection_detail'),
path('<str:colcat>/', views.collection_view, name='collection_view'),

Ruby convert Url parameters to array

I have this url encoded:
Started PUT "/path/thing/9812/close?status=close&shutdown_on=2018-12-05%2010%3A08%3A06&affected_external_id=15027&fqdns%5B0%5D=150.212.3.249"
which decoded is this:
"/path/thing/9812/close?status=close&shutdown_on=2018-12-05 10:08:06&affected_external_id=15027&fqdns[0]=150.212.3.249"
I get this parameters:
Parameters: {"status"=>"close", "shutdown_on"=>"2018-12-05 10:08:06", "affected_external_id"=>"15027", "fqdns"=>{"0"=>"150.212.3.249"}, "id"=>"9812"}
How can get fqdn as a array? on Rails 4
You should do the following:
params[:fqdns].to_a
Doing this, will produce the following:
{['0', '150.212.3.249', ...]}
If you wnat only the values, may you can try:
params[:fqdns].values
Doing this, will give you the following:
['150.212.3.249', ...]
But for this, you have to do it inside a ruby class, i strongly recommends you to do it inside your controller. Hope i can help.
UPDATE
After a recommends, you can do it with strong parameters, permiting the param fqdns as a hash (because you route receiving a hash):
def resource_params
params.permit(....., fqdns: {})
end
After this, you already have to execute the solutions above to get fqsnd as a array

Ignore params in urls

I need to bolt a quick city-specific thing onto a site I am currently building. I am going to do it something like this - http://example.com/XX/normal-slug. What I have set up in my urls.py is this:
url(r'^(?P<city>[a-zA-Z]{2})/', include('homepage.urls', namespace='homepage')),
url(r'^(?P<city>[a-zA-Z]{2})/section/', include('section.urls', namespace='section')),
# etc
The problem I am encountering now is that all of a sudden my methods all are now expecting a "city=XX" param. I plan to process the actual city business logic in a middleware. My question is... is there anyway have django "ignore" the named param? I don't want to modify all my views now to take either **kwards or 'city' param. If I hard code the city code, it does what I expect:
url(r'^XX/section/', include('section.urls', namespace='section')),
So can I replicate that behaviour, but dynamically?
(Also, I plan on something more robust further down the line, probably Django Sites)
You can use a non-capturing regex to accept the parameter but not pass it to the views.
r'^[a-zA-Z]{2}/section'
Set the param as optional in the regexp with ?:
url(r'^((?P<city>[a-zA-Z]{2})/)?section/', include('section.urls', namespace='section')),
If city is not sent in the URL, your view will receive city=None

Parse soap response and concatenate with another string

I have been using soapui opensource for a small period and not yet good at groovy script. Please help figuring out the following issue:
I get response from the previous test step. Lets say Response1 and need to parse it in order to get Id value from it. Then I need to add string DomainId before this id so that it looked smth like this:
DomainId_234565
and tranfer it to next request.
Could someone please explain how to do it with groovy? (I guess it is the best way to do it)
Thank you
Managed to resolve myself. Add property step response where I store response from previous step and also added property trasfer step to put response to the property. Then I add groovy script: def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context ) def holder = groovyUtils.getXmlHolder("Properties#response") return "DomainId_ " + holder.getNodeValue("//*:Id") and it works, returns the correct value

Mapping urls in routes.py

I am using web2py and builing a REST api and have one of my URLs set up like this:
routes_in (
('/myapp/something/{?P<id>.*)/myfunction', /myapp/default/myfunction/\g<id>')
)
routes_out = (
('/myapp/default/myfunction/\g<id>', '/myapp/something/{?P<id>.*)/myfunction')
)
If my app is setup this way my function is not even entered into and I get an invalid request if I remove the id argument from the url that my url is being mapped to i.e. remove g<id> from above, I enter my function but the argument is not being captured.
I cannot change the structure of the URL as per my requirements and I am not sure how to go about this.
I would appreciate any pointers.
Thanks,
nav
The above does work in web2py I found that some other area of my code was breaking.