Laravel: Routes with parameters - laravel-5.4

I have following case:
Route::get('/kids_report_card/{id?}/{param?}', 'KidsReportCardController#index');
And in view file I have:
{{ url('kids_report_card/4') }}
In some other view file I have:
{{ url('kids_report_card/name') }} (where name is string here-some parameter)
Now the situation is:
For {{ url('kids_report_card/4') }} ,Route::get('/kids_report_card/{id?}/{param?}', 'KidsReportCardController#index'); works fine.
For {{ url('kids_report_card/name') }},Route::get('/kids_report_card/{id?}/{param?}', 'KidsReportCardController#index'); doesn't work fine as in url we have name parameter while in Route we have first parameter as id(integer value). so is there any dynamic solution that srting parameter must go to second parameter in Route??

You can pass an array of parameters in the url like this:
{{ url('kids_report_card', ['name' => 'name_value']) }}
Reference: URL's

Related

set data in html django

I learning django rest. And now i wont output some json data in html. My json:
{'Resul': {'Period Start': '2017-01-01', 'Period End': '2017-12-12'}}
then i send it json to html:
context = {'Resul': json_data['date']}
content = render_to_string('balance.html', context)
json_data['date'] - {'Period Start': '2017-01-01', 'Period End': '2017-12-12'}
in html i write this code
Period: {{ Resul['Period Start'] }} - {{ Resul['Period End'] }}
but have error:
Could not parse the remainder: '['Period Start']' from 'Resul['Period Start']'
I strongly recommend you not to use spaces in dictionary key names, change them for underscores and do it like this:
Period: {{ Resul.Period_Start }} - {{ Resul.Period_End }}

Jekyll Liquid Array ID Not Working

I have a CSV file in my _data folder and I am trying to specify the row of the CSV to access in my Front Matter and then return specific columns from the CSV based on the row specificed in the Front Matter.
Here's the CSV file:
name,description
Dallas,Big City in Texas
And here's the contents of my index.html file:
---
city: "Dallas"
---
{{ site.data.data[page.city].description }}
Per the Jekyll Docs page on using data files, I should be able to use this syntax to access data files in this way, but the compiled html file does not include any data.
I have tested other ways of accessing the contents of the CSV file and those work, so it doesn't appear to be a problem with the data file or the site itself but rather with using the [ ] array id Liquid syntax.
Looks like you have misunderstood the [] notation for a hash structure.
I will first orient you on how the [] is supposed to work..
Expanding your data.csv a bit:
name,description
Dallas,Big City in Texas
Houston,Another City in Texas
and "inspecting" your the data object obtained from the above CSV file,
{{ site.data.data | inspect }}
on building the site, you'll see that the resultant object is simply an Array of Hashes :
<p>
[
{"name"=>"Dallas", "description"=>"Big City in Texas"},
{"name"=>"Houston", "description"=>"Another City in Texas"}
]
</p>
which means you can only access individual hash entry by referencing its index number.
i.e. {{ site.data.data[0] }} will give you the first hash and {{ site.data.data[1] }} will give you the next hash.
and therefore {{ site.data.data[0].description }} will give you the result you expect to get:
<p>
Big City in Texas
</p>
Now that you know how [] works for data hashes, lets simply get to the solution.
To access elements in an Array, one can simply iterate through the Array objects and reference necessary entries:
{% for entry in site.data.data %}
<div>
<span>{{ entry.name }}</span> : <span>{{ entry.description }}</span>
</div>
{% endfor %}
will give you:
<div>
<span>Dallas</span>
<span>Big City in Texas</span>
</div>
<div>
<span>Houston</span>
<span>Another City in Texas</span>
</div>

Angularjs web app error

The angularjs app doesn't show me any result.
It shows me :
Name City Order Total joined
{{ cust.name }} {{ cust.city }} {{ cust.orderTotal }} {{ cust.joined }}
What is the reason of this type of error !!!
Update 1:
function CustController($scope)
{
$scope.sortBy = 'name';
$scope.reverse = false;
$scope.customers = [
{joined: '240-344-55', name:'jone', city:'usa', orderTotal:'231'},
{joined: '240-344-55', name:'jone', city:'usa', orderTotal:'231'},
{joined: '240-344-55', name:'jone', city:'usa', orderTotal:'231'}
];
$scope.doSort = function(propName) {
$scope.sortBy = propName;
$scope.reverse = !$scope.reverse;
};
}
If you are using $scope approach, then you have to remove "cust." part from your view. It would be {{ name }} {{ city }} etc.
But if your view has ng-controller="CustController as cust", that means you are using "controller as" syntax, so you would need to refactor your controller code, changing $scope. to this. everywhere at least.

#1067 - Invalid default value for 'remember_token'

In the Lynda tutorial "Up and Running with Laravel", a sample app (authapp) is created, allowing user to log in.
Here is the structure of the create_user migration:
public function up()
{
Schema::create('users', function($newtable)
{
$newtable->increments('id');
$newtable->string('email')->unique();
$newtable->string('username',100)->unique();
$newtable->string('password',50);
$newtable->string('remember_token',100);
$newtable->timestamps();
});
}
And here is the corresponding view file excerpt:
{{ Form::open(array('url'=>'register')) }}
{{ Form::label('email', 'Email Address') }}
{{ Form::text('email') }}
{{ Form::label('username', 'Username') }}
{{ Form::text('username') }}
{{ Form::label('password', 'Password') }}
{{ Form::password('password') }}
{{ Form::submit('Sign Up')}}
{{ Form::close() }}
When everything is saved and I try to register in the actual app, I get an error: the tutorial says it's because there is no default value set for the remember_token variable and suggests to go to phpMyAdmin to set the default value to NULL.
However, when I do so, I get the following error message:
Consequently, I cannot fix the error and proceed.
Any idea on how to deal with this situation?
In phpMyAdmin, you need to check the Null checkbox as well, to allow null values in this field.
It's barely cutoff on the right side of your screenshot.
I just can see that the Null checkbox is not checked. Set the field as nullable by checking the Null checkbox as well.

Create hyperlink in django template of object that has a space

I am trying to create a dynamic hyperlink that depends on a value passed from a function:
{% for item in field_list %}
<a href={% url index_view %}{{ item }}/> {{ item }} </a> <br>
{% endfor %}
The problem is that one of the items in field_list is "Hockey Player". The link for some reason is dropping everything after the space, so it creates the hyperlink on the entire "Hockey Player", but the address is
http://126.0.0.1:8000/Hockey
How can I get it to go to
http://126.0.0.1:8000/Hockey Player/
instead?
Use the urlencode filter.
{{ item|urlencode }}
But why are you taking the name? You should be passing the appropriate view and PK or slug to url which will create a suitable URL on its own.
Since spaces are illegal in URLs,
http://126.0.0.1:8000/Hockey Player/
is unacceptable. The urlencode filter will simply replace the space with %20, which is ugly/inelegant, even if it does kind of get the job done. A much better solution is to use a "slug" field on your model that represents a cleaned-up version of the title field (I'll assume it's called the title field). You want to end up with a clean URL like:
http://126.0.0.1:8000/hockey_player/
To make that happen, use something like this in your model:
class Player(models.Model):
title = models.CharField(max_length=60)
slug = models.SlugField()
...
If you want the slug field to be pre-populated in the admin, use something like this in your admin.py:
class PlayerAdmin(admin.ModelAdmin):
prepopulated_fields = {"slug": ("title",)}
....
admin.site.register(Player,PlayerAdmin)
Now when you enter a new Player in the admin, if you type "Hockey Player" for the Title, the Slug field will become "hockey_player" automatically.
In the template you would then use:
{% for item in field_list %}
<a href={% url index_view %}{{ item.slug }}/> {{ item }} </a> <br>
{% endfor %}
There is this builtin filter .
http://docs.djangoproject.com/en/dev/ref/templates/builtins/#urlencode
Although you should be using one of these
http://docs.djangoproject.com/en/dev/ref/models/fields/#slugfield