Hey I am working on blog project and I have created a form to add post and I want to use my css with froms.In add post form there is a image field to upload image .
forms.py
class AddPostForm(ModelForm):
class Meta:
model = Post
fields = {'title_image'}
labels ={
'title_image':'Upload Main Image'
}
addpost.html
<form action= "#" method="post" enctype="multipart/form-data">
<div class="file-upload-wrapper" data-text="Select your file!">
{{ form.title_image }}
</div>
</form>
how to use my css classes I already tried widgets and got error
'title_image' : forms.ImageField(attrs ={'class':'form-control'})
did something like this but got error
Field.init() got an unexpected keyword argument 'attrs'
Thanks and any advice will be helpful.
Related
I have a Laravel application. One of the pages can be reached via the following URL
http://localhost:8000/items/gallery?item_type=glasses
As the amount of items to be shown can be quite substantial, I'm using pagination. I have the following code in my view:
#foreach($media as $media_item)
<div class="col-md-3">
<div class="card">
<img class="card-img-top" src="{{ asset('storage/'.$media_item->id .'/'. $media_item->file_name) }}" ">
</div>
</div>
#endforeach
{{ $media->links() }}
and in the controller, I'm using:
$media = Media::paginate(5);
The pagination buttons are shown and work for the 1st one. Then when I click on the second (or third or fourth...) one, I get the following error message:
Method Illuminate\Database\Eloquent\Collection::links does not exist.
I see the link is trying to reach:
http://localhost:8000/beeritems/gallery?page=2
whereas I need:
http://localhost:8000/beeritems/gallery?item_type=glasses&page=2
In Laravel, how can I change the links() method to include the part after the question mark?
You must use ->appends() methods
$media = Media::paginate(5);
$media->appends($request->all());
you can use laravel basic URLs instead of getting gallery images with URL get parameters.
something like this:
define Route like this
/items/gallery/{types}
then using it like
http://localhost:8000/items/gallery/glasses
in this case you don't get that error anymore
I am making a simple put request to my app backend using axios.put();
This all works, I have a button that is binded to vue like #click="submitForm"
However looking around I see that some people still wrap their input fields in forms like those:
<form method="POST" #submit.prevent="onSubmit" action="{{ route('someRoute') }}" id="form-submit">
{{ method_field('PUT') }}
{{ csrf_field() }}
Even if I dont use a form like the one above I get the same result when calling my ajax put request.
Laravel allready adds csfr headers to axios by default in resources/assets/js/bootstrap.js
So is there any reason I still should wrap my inputs in a form like above?
Thanks
Your ajax request doesn't matter if you do your inputs in form tags or not, because the request still sends and receives data from a server.
I would use a form tag because everybody can read the markup much easier and it could be usefull for writing less code in javascript - one example
<form action="" method="">
<div class="form-group">
<label class="control-label">Input</label>
<input type="text" name="input" />
</div>
</form>
$(document).ready(function() {
$('#some-form').on('submit', function() {
var data = $(this).serialize();
... do whatever you want (like ajax call) ...
return false;
});
);
You're using an Ajax request, in which case a standard "form submit" would get prevented. Putting a form around it is not obligatory, especially if you use a button element, which is not a classic form element anyway.
I am using Laravel 5.2 and package "barryvdh/laravel-dompdf". I am streaming/viewing the view file like this:
$manu = Mmanufacturer::select('manName')->find($manuid);
$prds = compact('prds','images','temp_header','temp_footer');
return PDF::loadView('cp.reports.letters-pdf', $prds)->stream();
This opens the appropriate view and is working fine... see image:
http://prntscr.com/dflj4d
At the end of url as you see '43' which is id by default..what I want to show at the end is manufacturer/product or something name instead of its id..Can we achieve this?
You can set custom routing for view pdf, send parameter by post method. Like:
Route::post('/manufacturer/product', 'ReportController#viewPdf');
If you use button then use this way:
View Pdf
Form For View PDF
<form id="viewPdf" action="{{ url('/manufacturer/product') }}" method="POST" style="display: none;">
<input type="hidden" name="product_id" value="43">
{{ csrf_field() }}
</form>
In Controller:
$manuid= $request->product_id;
$manu = Mmanufacturer::select('manName')->find($manuid);
$prds = compact('prds','images','temp_header','temp_footer');
return PDF::loadView('cp.reports.letters-pdf', $prds)->stream();
Hope this trick will help. Thank You :)
Currently I'm getting some data from the database and after that I want to render it within my Blade template.
In my queried data I have blade functions like url('/foo') combined with some html. And here is the problem.
When I'm using {!! $child->description !!} the HTML is rendered correctly, but my Blade function won't work:
Function: url('/foo)
Output: http://myurl.de/url('/foo')
When I'm using the "normal" Syntax like {{ $child->description }} the generated URL is correct (http://myurl.de/foo), but the HTML is not rendered.
So my question is:
How can I use my queried Blade function within rendered HTML? ^^
/Edit
Okay, perhaps my question is too abstract. So I want to show you my problem based on my example. (generated template image - only on german, sorry)
Every form is a database entry like:
categoryName
categoryParent
...
categoryDescription
As you can see on my image, the categoryDescription is the small text under my first input field with the small button.
I want to use this script abstract as possible so that I can fill the entry with every content I want to fill in.
In this case my content is:
lore ipsum <a class="btn btn-primary btn-sm pull-right" href="url('foo')">dolor</a>
As you can see there is the mentioned Blade-function (url) and the HTML.
{!! !!} - this dont escapse string so if u have something from database like,
something it would output it like that.
While in other hand {{ }} this would give you just "something" without , it is used to protect you from injections.
Maybe blade error.{{}}
lore ipsum <a class="btn btn-primary btn-sm pull-right" href="{{url('foo')}}">dolor</a>
Laravel Blade
I've seen this asked a few times but have never seen an answer. I have a Django ModelForm with a hidden input field. I need to use info from my template for the hidden field - the 'send_to' field in this case.
So I have the form where users can write a subject and a message, and I want to autopopulate the "to" field.
This is my modelform:
class MessageForm(ModelForm):
class Meta:
model = Message
fields = ['subject', 'message', 'send_to']
widgets = {'send_to': forms.HiddenInput()}
The 'send_to' part needs to be populated with info from my template.
<form method="post" action="{% url 'send' %}" id="commiserator" name="{{ c.id }}" class="write-to-user" enctype="multipart/form-data">
<h3>To: {{ c.username }}</h3>
{% csrf_token %}
{{message.as_p}}
<input id="commiserator" class="write-to-user" type="submit" value="Send">
Right now {{c.username}} is just displayed as html. I want it to be used as form input for the hidden field, but I have no idea how to do that.
The form is being sent to the view with Ajax so I would like to make it so that the hidden information is populated before Ajax sends the data. Hope that makes sense.
You can't do it directly from the template. But you have a couple of options.
In your view where you instantiate your message ModelForm to pass to the template, you could pre-populate the value of the send_to field. This should then render it in the template with that same value. This is probably what you want.
Your other option is to exclude send_to from the ModelForm, and to add it hard-coded as a hidden field in the form in your template. {{message.as_p}} will then only render the visible fields.