Django forloop not indexing correctly - html

I am populating a table from a python list passed through a django tag:
{% for alt in altList %}
<td align="center">
{{alt.1}}</br>
{{alt.2}}</br>
{{alt.3}}</br>
{{alt.4}}</br>
<input type="hidden" value={{alt.0}}/>
</td>
{% endfor %}
This works correctly, but the list is randomly generated in python and I need to maintain the same list if the user of my form makes an error. I included a hidden field that stores the current list. The trouble is that, when I have an error, it does not run the loop as I expect.
The working input list is shown as:
[(196, u'hydro', u'25', u'735', u'7'), (266, u'coal', u'140', u'0', u'63'), (372, u'hydro', u'260', u'990', u'63'), (383, u'solar', u'510', u'990', u'63')]
When I have a list from the previous post I would like to use, it is also shown as follows in the console:
[(196, u'hydro', u'25', u'735', u'7'), (266, u'coal', u'140', u'0', u'63'), (372, u'hydro', u'260', u'990', u'63'), (383, u'solar', u'510', u'990', u'63')]
It appears to have the same formatting, so I'm not sure what the issue could be; however, in the second case it parses very differently and gives me a long list that includes things like ''/' as elements.

You could save the list's random order in user's session or I think it's a better choice to shuffle in the client side through javascript.

Related

How to get a I18n variable value I can return to my Angular parent component?

I'm new to Angular and I just put in place an i18n (2 languages) system for a website I am creating. Everything works properly but in order to switch from one language to another in my header, I feel stuck.
I followed the Angular documentation to transfer my variables from child to parent component and I ended with this:
<input type="text" id="item-input" #lang>
<button type="button" (click)="changeChosenLang(lang.value)">
{{ 'global.lang' | translate }}
</button>
As you can see, I write my language in the input form and I send it to the proper component with a button. What I wanted was to click on my 'global.lang' text and to be able to send its value to the parent component, since the value is the language which is not actually used.
I don't know how to put my 'global.lang' text in a variable, neither what kind of balise I can use. Also I didn't know how to summarize my problem to search for it on StackOverflow so if you know a similar post, don't hesitate to post the link.
Thank you for your reading!
I found a less tortured way (poor brain) to have the result I wanted:
<span (click)="changeChosenLang()">
{{ 'global.lang' | translate }}
</span>
First I temporary changed my button to a span balise and I deleted the parameter from my changeChosenLang() function. Then, I transferred a variable 'lang' from my parent component to this one, witch contains the value of the language chosen in my app constructor. At each click, I change its value in my changeChosenLang() function and everything works great!
I hope it can help someone someday. The moral of this post is: the simpler, the better! Have a good day.

Django Admin: How to use the button to move to the selected URL?

I am very new to Django and trying to make my first project. I find it difficult to use the buttons I created to move to the selected URL.
Let's say my app is called TestForms and my models are: Patients, General, PST and ERBT. I would like to create two buttons - 'Previous' and 'Next' - which will be used to go to previous/next forms respectively. I try to do so using admin templates in django.
NOTE: I know changing built-in templates are not a very good idea, I will create new html file to extend this templates before doing changes on the server. For now I am doing it locally on my computer.
In submit_line.html I created two new buttons and they are like so:
{% if show_save_and_go_to_next_form %}<input type="submit" value="{% translate 'Next' %}" class="default" name="_gotonextform">{% endif %}
{% if show_save_and_go_to_previous_form %}<input type="submit" value="{% translate 'Previous' %}" class="default" name="_gotopreviousform">{% endif %}
This gives me two good-looking buttons on the site.
But these are just saving the results (working like 'Save' button), but not redirecting me to the next form as I would like to. When I am adding a new patient (admin/TestForms/patient/add/), after clicking on 'Next' I would like the server to save this patient and redirect me to admin/TestForms/general/add/ to be able to fullfil the next form, then save the changes and move on to admin/TestForms/PST/add/and so on.
I know I have to add the anchor, but I tried multiple times with different approaches and nothing worked. When I try to use <a href ...>, the button disappears. Also it is difficult for me to figure out how to move from one form to another and to disable the 'Previous' button on the first form and the 'Next' button on the last form.
Any suggestions how to achieve it?
The redirect needs to be done in your view, not in the template.
def your_view(request, *args, **kwargs):
# your code ...
if request.POST.get('_gotonextform'):
return redirect('admin/TestForms/general/add/')
else:
# do whatever you like if any other button was clicked
pass

Getting a route not defined error in Laravel Blade

I am a Laravel/web newbie, and am working with a Laravel application inherited from someone else. In the blades, I see two ways in which forms are written.
<form action="{{ route('getdata') }}"
{!! Form::open(array('url'=>'getdata','
I think in the first case, the form starts in HTML format and route alone is defined in laravel format whereas in the second case, even form is defined in Laravel format. Which is preferred and what are the differences? I tried replacing the second format with the first and got a Route [/getdata] not defined error. Laravel Version is 6.
In the first example:
<form action="{{ route('getdata') }}"
You are writing plain html, except for the {{ route('getdata') }} part, that you can read similar to <?php route('getData') ?>. Basically, just the action (the url) of the form is a PHP call to a function that will echo the url in that position, while the rest is plain html (an hardcoded text).
In the second example, {!! Form::open(array('url'=>'getdata',' you are using a Facade to access a class that will generate an html output (similar to the code from the first example ) and you are passing to the method Open() an url, that will be placed inside the generated html in the action field.
The problem that you have using the second method, is that your are not passing your route, but a string. Change it like this:
{!! Form::open(array('url'=>route('getdata'),'
To fix the error of the undefined route, you should just call name()function at the end of your route:
Route::('your_route_url','controller#method')->name('getdata');
You didn't define any route named getdata acctually.
Change your code route to url :
<form action="{{ url('getdata') }}

How to delete data from database inline using Symfony

I'm new with Symfony and I'm trying to delete some data from the database using an html table, something that looks like this but I don't know where to start:
Any help is appreciated :)
There are basically 3 choices, whether using symphony or not:
A link to a page (look at Symfony "routes") including the information which entry to delete.
A nice trick is to use the same page and add a query parameter.
This would look something like this in twig:
{% for row in table %}
delete
{% endfor %}
And in symfony:
if($request->get->has('delete'))
{
//delete it from the database
}
//the code that displays your table
This is the easiest way but has a few disadvantages. It is slow as the whole page has to be loaded again. The browsers back-button might not behave as the user might expect.
A html form doing basically the same as 1. but with post
{% for row in table %}
<form action="" method="POST">
<input type="hidden" name="delete" value="{{row.id}}">
<input type="submit" value="delete">
</form>
{% endfor %}
And in symfony use $request->request
Ajax triggered by an onclick event
A javascript deleterow function would send the id via ajax to a special symphony route that returns an JSONResponse and when successful delete the DOM Element with the row.
This is much more complicated and would need a lot more research on your part, but it is the modern way.

Preferred way to POST with params from a listing template in Django

I was looking for some input on the preferred way of making a post request from a list template in Django. The way I'm currently using doesn't quite seem right to me, even though it works.
Let's say I have a table of data from my model, SomeModel, which I'm outputting in a Django GCBV ListView.
class SomeModelList(ListView):
model = SomeModel
In my template I'm iterating over the list of models and outputting each row in a table with some of the data, and I want two buttons which make a POST to perform some operation on the current row's object.
I can write an entire form for each operation manually. Something like:
{% for object in object_list %}
{{ object.name }}</br>
<form method="POST" action="{{ url 'do_something' {{ object.pk }}"><input type="button" value="Do Something"/></form></br>
<form method="POST" action="{{ url 'do_something_else' {{ object.pk }}"><input type="button" value="Do Something Else"/></form></br>
{% endfor %}
It doesn't seem correct to be writing out these form tags like this.
Is this really the best way? Maybe I should have a single giant form around the whole list and use some properties of the button? Maybe I should be doing something with Django forms? Maybe something else entirely?
Thanks!