How to get image value from Django form? - django-forms

<div class="card-text">
<img id="image_upload_preview" {% if adminform.form.photo.? %}src="{{ adminform.form.photo.? }}"{% endif %} />
<input type='file' id="inputFile" name="photo" class="img-choose" />
</div>
how to get value of image in the above code

<div class="card-text">
<img id="image_upload_preview" {% if adminform.form.photo.value %}src="{{ adminform.form.photo.value }}"{% endif %} />
<input type='file' id="inputFile" name="photo" class="img-choose" />
</div>

Related

Stock Availability toggle liquid

I have a store with an availability show hide toggle set to hide by default but i'd like to change the default to show. I have tried editing the below code but can seem to get it right. Any help would be greatly appreciated.
<div class="filter-group filter-group--availability">
<div class="filter-toggle filter-toggle--inline">
<span class="filter-toggle__group-label">{{ 'collections.filtering.out_of_stock_label' | t }}</span>
<div class="filter-toggle__options">
<label class="filter-toggle__input-label">
<input class="filter-toggle__input"
id="Filter-{{ filter.param_name }}-1"
type="radio"
name="{{ filter.param_name }}"
value="2"
{% if filter.active_values != empty and filter.active_values.first.value == '2' %}checked{% endif %}>
<span class="filter-toggle__input-label-text">{{ 'collections.filtering.show_label' | t }}</span>
</label>
<label class="filter-toggle__input-label" id="second-filter-btn">
<input class="filter-toggle__input"
id="Filter-{{ filter.param_name }}-2"
type="radio"
name="{{ filter.param_name }}"
value="1"
{% if filter.active_values == empty %}checked{% endif %}>
<span class="filter-toggle__input-label-text">{{ 'collections.filtering.hide_label' | t }}</span>
</label>
</div>
</div>
</div>

How to create a yes no filter option on custom product option shopify liquid

I have products with an option set of initials, so a-z on the frontend category filter. This is obviously very long when expanded and was wondering if it could be set to a yes/no boolean instead?
Edit. The below image is what i'm looking to duplicate and have attached the code for that. I'm guessing I could copy but change the param name? but not sure what the param name would be.
{%- if filter.param_name == 'filter.v.availability' -%}
<div class="filter-group filter-group--availability">
<div class="filter-toggle filter-toggle--inline">
<span class="filter-toggle__group-label">{{ 'collections.filtering.out_of_stock_label' | t }}</span>
<div class="filter-toggle__options">
<label class="filter-toggle__input-label">
<input class="filter-toggle__input"
id="Filter-{{ filter.param_name }}-1"
type="radio"
name="{{ filter.param_name }}"
value=""
{% if filter.active_values == empty %}checked{% endif %}>
<span class="filter-toggle__input-label-text">{{ 'collections.filtering.show_label' | t }}</span>
</label>
<label class="filter-toggle__input-label" id="second-filter-btn">
<input class="filter-toggle__input"
id="Filter-{{ filter.param_name }}-2"
type="radio"
name="{{ filter.param_name }}"
value="1"
{% if filter.active_values != empty and filter.active_values.first.value == '1' %}checked{% endif %}>
<span class="filter-toggle__input-label-text">{{ 'collections.filtering.hide_label' | t }}</span>
</label>
</div>
</div>
</div>
{%- else -%}

Laravel Jetstream Livewire undefined variable

I duplicated <x-slot name="form"> with <x-slot name="form2"> and inserted variable $form2 in form-section.blade.php but get an error for undefined $form2. I don't know why update-profile-information-form.blade.php didn't send $form2 HTML. I am using Laravel 8.
Error: ErrorException Undefined variable: form2 (View:
\resources\views\vendor\jetstream\components\form-section.blade.php)
form-section.blade.php
#props(['submit'])
<div {{ $attributes->merge(['class' => '']) }}>
<x-jet-section-title>
<x-slot name="title">{{ $title }}</x-slot>
<x-slot name="description">{{ $description }}</x-slot>
</x-jet-section-title>
<div class="md:grid md:grid-cols-2 md:gap-6">
<div class="mt-2 md:mt-0 md:col-span-1">
<form wire:submit.prevent="{{ $submit }}">
<div class="px-4 py-5 bg-white sm:p-6 shadow {{ isset($actions) ? 'sm:rounded-tl-md sm:rounded-tr-md' : 'sm:rounded-md' }}">
<div class="grid grid-cols-6 gap-6">
{{ $form }}
</div>
</div>
#if (isset($actions))
<div class="flex items-center justify-end px-4 py-3 bg-gray-50 text-right sm:px-6 shadow sm:rounded-bl-md sm:rounded-br-md">
{{ $actions }}
</div>
#endif
</form>
</div>
<div class="mt-2 md:mt-0 md:col-span-2">
<form wire:submit.prevent="{{ $submit }}">
<div class="px-4 py-5 bg-white sm:p-6 shadow {{ isset($actions) ? 'sm:rounded-tl-md sm:rounded-tr-md' : 'sm:rounded-md' }}">
<div class="grid grid-cols-6 gap-6">
{{ $form2 }}
</div>
</div>
#if (isset($actions))
<div class="flex items-center justify-end px-4 py-3 bg-gray-50 text-right sm:px-6 shadow sm:rounded-bl-md sm:rounded-br-md">
{{ $actions }}
</div>
#endif
</form>
</div>
</div>
</div>
update-profile-information-form.blade.php
<x-jet-form-section submit="updateProfileInformation">
<x-slot name="title">
{{ __('Profile Information') }}
</x-slot>
<x-slot name="description">
{{ __('Update your account\'s profile information and email address.') }}
</x-slot>
<x-slot name="form">
<!-- Name -->
<div class="col-span-6 sm:col-span-4">
<x-jet-label for="name" value="{{ __('Name') }}" />
<x-jet-input id="name" type="text" class="mt-1 block w-full" wire:model.defer="state.name" autocomplete="name" />
<x-jet-input-error for="name" class="mt-2" />
</div>
<!-- Email -->
<div class="col-span-6 sm:col-span-4">
<x-jet-label for="email" value="{{ __('Email') }}" />
<x-jet-input id="email" type="email" class="mt-1 block w-full" wire:model.defer="state.email" />
<x-jet-input-error for="email" class="mt-2" />
</div>
</x-slot>
<x-slot name="form2">
<!-- Profile Photo -->
#if (Laravel\Jetstream\Jetstream::managesProfilePhotos())
<div x-data="{photoName: null, photoPreview: null}" class="col-span-6 sm:col-span-4">
<!-- Profile Photo File Input -->
<input type="file" class="hidden"
wire:model="photo"
x-ref="photo"
x-on:change="
photoName = $refs.photo.files[0].name;
const reader = new FileReader();
reader.onload = (e) => {
photoPreview = e.target.result;
};
reader.readAsDataURL($refs.photo.files[0]);
" />
<x-jet-label for="photo" value="{{ __('Photo') }}" />
<!-- Current Profile Photo -->
<div class="mt-2" x-show="! photoPreview">
<img src="{{ $this->user->profile_photo_url }}" alt="{{ $this->user->name }}" class="rounded-full h-20 w-20 object-cover">
</div>
<!-- New Profile Photo Preview -->
<div class="mt-2" x-show="photoPreview">
<span class="block rounded-full w-20 h-20"
x-bind:style="'background-size: cover; background-repeat: no-repeat; background-position: center center; background-image: url(\'' + photoPreview + '\');'">
</span>
</div>
<x-jet-secondary-button class="mt-2 mr-2" type="button" x-on:click.prevent="$refs.photo.click()">
{{ __('Select A New Photo') }}
</x-jet-secondary-button>
#if ($this->user->profile_photo_path)
<x-jet-secondary-button type="button" class="mt-2" wire:click="deleteProfilePhoto">
{{ __('Remove Photo') }}
</x-jet-secondary-button>
#endif
<x-jet-input-error for="photo" class="mt-2" />
</div>
#endif
</x-slot>
<x-slot name="actions">
<x-jet-action-message class="mr-3" on="saved">
{{ __('Saved.') }}
</x-jet-action-message>
<x-jet-button wire:loading.attr="disabled" wire:target="photo">
{{ __('Save') }}
</x-jet-button>
</x-slot>
</x-jet-form-section>
I just found the problem, in form-section.blade.php you have: #props(['submit', 'test' => '']) if you don't put default value for that variable test, the variable we'll be see it like undefined

Livewire profile edit form content on the page does not change

I edited the laravel livewire profile and updated the blade in the livewire resource and the content on the page does not change. How I can update content on page after changing code? I can remove all this code, but it's still remaining on page:
<x-jet-form-section submit="updateProfileInformation">
<x-slot name="title">
{{ __('Profile Information') }}
</x-slot>
<x-slot name="description">
{{ __('Update your account\'s profile information and email address.') }}
</x-slot>
<x-slot name="form">
<!-- Profile Photo -->
#if (Laravel\Jetstream\Jetstream::managesProfilePhotos())
<div x-data="{photoName: null, photoPreview: null}" class="col-span-6 sm:col-span-4">
<!-- Profile Photo File Input -->
<input type="file" class="hidden"
wire:model="photo"
x-ref="photo"
x-on:change="
photoName = $refs.photo.files[0].name;
const reader = new FileReader();
reader.onload = (e) => {
photoPreview = e.target.result;
};
reader.readAsDataURL($refs.photo.files[0]);
" />
<x-jet-label for="photo" value="{{ __('Photo') }}" />
<!-- Current Profile Photo -->
<div class="mt-2" x-show="! photoPreview">
<img src="{{ $this->user->profile_photo_url }}" alt="{{ $this->user->name }}" class="rounded-full h-20 w-20 object-cover">
</div>
<!-- Name -->
<div class="col-span-6 sm:col-span-4">
<x-jet-label for="name" value="{{ __('Name') }}" />
<x-jet-input id="name" type="text" class="mt-1 block w-full" wire:model.defer="state.name" autocomplete="name" />
<x-jet-input-error for="name" class="mt-2" />
</div>
</x-jet-form-section>

Get template block contents and call from ajax

I'm using django 1.9 and python 3. My english also isn't the best, so excuse the question if it is formulated bad.
I'm working on making my website a single-page application. I need to get the {% block %} contents of a given template, and then send that as a HttpResponse so that ajax can pick it up and inject it into the page.
I've tried using the answer from this question: Django - how to get the contents of a {% block %} tag from a template
But upon trying to fetch the contents of a block in my view like so:
response_data[content] = get_block_source('profile/login.html', 'content')
if request.is_ajax():
return HttpResponse(
json.dumps(response_data),
content_type="application/json"
)
I just get this error from django, no matter what I do:
ValueError at /login/ Template block content not found
The contents of the block don't even make it to the ajax call, what gives?
EDIT:
I'll include my "content" block here:
in my template:
{% extends 'base.html' %}
{% block content %}
<div class="big-title text">Log in</div>
<div class="form-container">
<form name="login-form" id="user" method="post" action="/login/" enctype="multipart/form-data" class="text">
{% csrf_token %}
<div class="title">Enter your credentials</div>
<div class="form-row">
<div class="form-flex">
<div class="field-container">
<div class="field-input-container">
<div class="field-label accent">Username</div>
<div class="field-input">
<input class="field-input-element" type="text" name="username" />
</div>
</div>
<div class="field-help">Your username is always lowercase.</div>
</div>
</div>
<div class="form-flex">
<div class="field-container" style="height: 110px">
<div class="field-input-container">
<div class="field-label accent">Password</div>
<div class="field-input">
<input class="field-input-element" type="password" name="password" />
</div>
</div>
<div class="field-help"></div>
</div>
</div>
</div>
<div class="form-button-container">
<div class="form-error"></div>
<div class="form-message"></div>
<input type="submit" name="submit" value="Accept" class="button form-button"/>
</div>
</form>
</div>
{% endblock %}
In my base (base.html)
<body>
<div id="modal-container">
<div id="modal-overlay">
<div id="modal-items">
</div>
</div>
</div>
<div id="wrapper">
<header>
<div id="header-title" class="text accent">App name</div>
<div id="header-nav">
<nav>
{% if user.is_authenticated %}
Home
Feed {% if request.user.is_superuser %}
Admin {% endif %}
N
<a href="/{{ request.user }}" class="header-avatar-small-a">
<div class="text header-greeting">Hi, {{ user.userprofile.display_name }}</div>
<div class="header-avatar-small">
{% if not user.userprofile.avatar == '' %}
<img src="{{ MEDIA_URL }}users/{{ user }}/avatar" alt=""> {% else %}
<img src="{{ MEDIA_URL }}users/avatar" alt=""> {% endif %}
</div>
</a>
{% else %}
Log in
Create account {% endif %}
</nav>
</div>
<div class="progress">
<div id="header-progress" class="fill"></div>
</div>
</header>
<main>
{% block content %} {% endblock %}
</main>
<footer></footer>
</div>
</body>
</html>
Template source is the template actual HTML, not the file reference

Resources