i cant add image to my new blog post in django - django-forms

kindly help me, i am using class based views now am about to create new post using Createview i added all the fields including an image which stands for the thumbnail so if i go to http://127.0.0.1:8000/pages/blog/new/ i get a form and if i fill in the fields and submit i get return back to the form saying the image fields is required meanwhile i already inserted an image , this is the error in picture
and this is my code below
views.py
class BlogCreateView(LoginRequiredMixin, CreateView):
model = Blog
fields = ['title', 'categories', 'overview', 'thumbnail', 'summary']
blog_form.html
<div class="content-section text-center">
<form method="POST">
{% csrf_token %}
<fieldset class="form-group ">
<legend class="border-bottom mb-4 h2">Blog Post</legend>
{{ form|crispy }}
</fieldset>
<div class="form-group">
<button class="btn btn-outline-info" type="submit">Post</button>
</div>
</form>
</div>

You need to add "enctype="multipart/form-data" to your form, so:
<form method="post" enctype="multipart/form-data">
See detailed explanation is this elaborate answer:
What does enctype='multipart/form-data' mean?

Related

Can't get id from previous page Laravel

I have a button to see detail about user using button in each row datatable, the button will call a route detail.show and open link with user_id with it (ex:http://127.0.0.1:8000/detail/7),
here is the button code
->addColumn('action', function ($post) {
$link = "route('detail.show',$post->user_id)";
$btn = ' <span class="fas fa-eye"></span>';
return $btn;
})
the problem is I cant get any data from it, is there something wrong?
here is my route
Route::resource('detail', DetailController::class);
Here is my controller
public function show($user_id)
{
$post = Post::where('user_id', $user_id)->latest()->get();
return view('detail', compact('post'));
}
and here is my view
#extends('layouts.app')
#section('content')
<div class="row mt-5 mb-5">
<div class="col-lg-12 margin-tb">
<div class="float-left">
<h2> Show Post</h2>
</div>
</div>
</div>
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>User ID:</strong>
{{ $post->user_id }}
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Name:</strong>
{{ $post->name }}
</div>
</div>
</div>
#endsection
I cant show anything using code as shown above. Is there something wrong? thanks in advance
Problems like this can often be solved by structuring your logic to be cleaner, more straightforward and use constructs of the framework as intended (documented).
In this case I would treat a user as resource and not the "detail".
I would rather create a specific route for showing the specific details of the user or just use partial resource routes.
Anyway, if you would like to keep your current build:
If I remember correctly, parameter passed to the show function in the controller should match the parameter name in your route.
When you generate routes with
Route::resource('detail', DetailController::class);
it creates something like
/details/{detail}
for the show method.
As described in this part of the docs.
Try replacing
$user_id
with
$detail
in your show method.

I cannot retrieve data from form to Django class. Cannot get data from object with using form field value

This is the HTML file
<form action="{% url 'invDrugs:invDrugs-search' %}" method="GET" id="form1"></form>
<div class="col-md-6">
<input type="text" placeholder="Search" name="search" class="form-control ">
</div>
<div class="col-md-2">
<button type="submit" form="form1" value="Submit"><i class="fas fa-search"></i></button>
</div>
</form>
this is the url path
path('search/', InvDrugsSearchView.as_view(), name='invDrugs-search')
this is the class
class InvDrugsSearchView(ListView):
model = InvDrugs
template_name = "invDrugs/search.html"
def get_queryset(self): # new
query = self.request.GET.get('search')
object_list = InvDrugs.objects.filter(Q(code=query))
return object_list
As you can see, I am submitting the form then it should be go to this class InvDrugsSearchView(ListView) but nothing happend.
I just want to get this value and search it from the DB.
You are closing the form tag right there in the first line. This means that the text field and submit button are not part of the form.

How to use laravel shweshi/OpenGraph

Here how to use it according to its GitHub
I already install the package:
Use Opengraph
$data = OpenGraph::fetch("http://www.addemyplus.com/blogs/my-faith-for-your-love");
that may return an array:
{"type":"website","title":"My Faith For Your Love","description":"","image":"http:\/\/www.addemyplus.com\/images\/frontend_images\/blogs\/medium\/57670.jpg"}
My problem is how can I use it on my blade that whenever I type a link in the form it will fetch the Og data of that site.
and show below the image and description
I have this code on form:
<form action="{{route('store_post_path')}}" method="POST">
{{csrf_field()}}
<div class="form-group">
<label class="sr-only" for="title">title</label>
<input type="text" class="form-control" name="title" placeholder="Your Post Title or Question">
</div>
<div class="form-group">
<label class="sr-only" for="post">post</label>
<textarea class="form-control" name="post_content" id="post_content" rows="3" placeholder="Briefly explain your question or Your Post Content"></textarea>
</div>
<div class="tab-pane fade" id="images" role="tabpanel" aria-labelledby="images-tab">
<div class="form-group">
<div class="custom-file">
</div>
</div>
<div class="py-4"></div>
</div>
<div class="btn-toolbar justify-content-between">
<div class="btn-group">
<button type="Submit" class="btn btn-primary">Share your Post</button>
</div>
</div>
</form>
How can I use the code to fetch the Og property of the link? I want to view the image and title of the link below before submitting my post.. Please show some tutorial sites for this.
Get the link from the form in post content. You can do it using regex.
function getUrl($string)
{
$regex = '/https?\:\/\/[^\" ]+/i';
preg_match_all($regex, $string, $match);
return ($match);
}
$url = getUrl($string)
If there are any links in post content you will get array of links.
Use OpenGraph::fetch($url) to fetch the OG data of the url. That you can show in the view.

not getting checkbox element value from form

I have a small form in Laravel 5.4 which has a checkbox and a text box. The issue is that when I post the form, the checkbox value is not coming through the request. I have custom styling on the checkbox but surely it can't be that?
I've been looking at this for a while, and everything looks normal. My code is below:
<form method="post" action="{{ route('admin.settings.save') }}">
{{ csrf_field() }}
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label><b>Site Name</b></label>
<p>This is the name of your LaravelFileManager instance.</p>
<input name="siteName" id="siteName" class="form-control" value="{{ \App\Helpers\ConfigHelper::getValue('site_name') }}" />
</div>
<div class="form-group">
<label><b>Footer Message</b></label>
<p>You can customise the footer message for the application.</p>
<div class="checkbox">
<label>
<input type="checkbox" name="showFooter" id="showFooter" checked="{{ \App\Helpers\ConfigHelper::getValue('show_footer_message') }}"> Show footer message
</label>
</div>
</div>
<button type="submit" class="btn btn-success"><i class="fa fa-save"></i> Save Changes</button>
</div>
</div>
</form>
My controller code is as such:
public function saveSettings(Request $request) {
$siteName = $request->input('siteName');
$showFooter = $request->input('showFooter');
ConfigHelper::setValue('site_name', $siteName);
ConfigHelper::setValue('show_footer_message', $showFooter);
return redirect()->route('admin.settings')->with('result', 'Settings saved.');
}
My route:
Route::post('settings/save', ['uses' => 'Admin\SettingsController#saveSettings'])->name('admin.settings.save');
I've also done a vardump on the $request variable and even that is missing the check box value:
array(2) {
["_token"]=> string(40) "sgyO7Kkz1ljsYEZ1G5nkj4uVbmFZqiTMbpK9P6Bi"
["siteName"]=> string(16) "File Manager 1.0"
}
It's missing the 'showFooter' variable.
Not quite sure where to go with this one. Any help appreciated.
So I got this working in the end. Using help from the comments:
public function saveSettings(Request $request) {
$siteName = $request->input('siteName');
$showFooter = $request->has('showFooter');
ConfigHelper::setValue('site_name', $siteName);
ConfigHelper::setValue('show_footer_message', $showFooter);
return redirect()->route('admin.settings')->with('result', 'Settings saved.');
}
For some reason, using $request->input('showFooter') wasn't working properly. $request->get('showFooter') brings a result when true, so adding the ternary makes it work every time.

Validation of invisible fields on submit

(Symfony 2.8)
I want to insert data into a form as steps in a process.
The field to validate is in "Descripción" tab and the submit buttons are in the "Guardar/Revisión" tab.
When I click the submit button, it displays the error "An invalid form control with name='form[proDescripcion]' is not focusable" in the Chrome debug console.
Is it possible to correct the error without changing the design?
<div id="tabs-1">
<p>Descripción</p>
<div class="inputText">
<div class="label">
<label for='proDescripcion' >{{ form_label(form.proDescripcion) }}</label>
</div>
<div class="input">
{{ form_widget(form.proDescripcion) }}
</div>
</div>
</div>
<div id="tabs-2">
<p>Imagen/Documento</p>
</div>
<div id="tabs-3">
<p>Datos</p>
</div>
<div id="tabs-4">
<p>Guardar/Revisión</p>
{{ form_row(form.save) }}
{{ form_row(form.finish) }}
</div>
You can add a "novalidate" attribute to your form
{{ form_start(contact, {attr: {novalidate: 'novalidate'}}) }}
With this attribute, HTML5 will not try to validate your form on submit. Don't forget to do it on the server-side with assert annotation.

Resources