我添加了一个基本的联系表格,如Shopify 文档中所述。
我的代码:
{% form 'contact' %}
{% if form.posted_successfully? %}
<div class="successForm feedback">
<p>Thanks for contacting us. We'll get back to you as soon as possible.</p>
</div>
{% endif %}
{% if form.errors %}
<div class="errorForm feedback">
<p>Sorry, we were unable to submit your inquiry because it contained {{ form.errors | size | pluralize: 'an error', 'a few errors' }}.<br/>Please correct the following and submit again:</p>
{% for field in form.errors %}
<p><strong>The {{ field | capitalize | replace: 'Body', 'Comments / questions' }} field {{ form.errors.messages[field] }}.</strong></p>
{% endfor %}
</div>
{% endif %}
<div id="contactFormWrapper">
<h3>Personal details</h3>
<ul>
<li>
<label>First name:</label>
<input type="text" id="contactFormFirstName" name="contact[first-name]" placeholder="" />
</li>
<li>
<label>Last name:</label>
<input type="text" id="contactFormLastName" name="contact[last-name]" placeholder="" />
</li>
<li>
<label>Email address:</label>
<input type="email" id="contactFormEmail" name="contact[email]" placeholder="" />
</li>
<li>
<label>Telephone number:</label>
<input type="telephone" id="contactFormTelephone" name="contact[phone]" placeholder="" />
</li>
<li>
<label>City:</label>
<input type="text" id="contactFormCity" name="contact[city]" placeholder="" />
</li>
<li>
<label>Country:</label>
<input type="text" id="contactFormCountry" name="contact[country]" placeholder="" />
</li>
</ul>
<h3>Items</h3>
<ul>
<li>
<label>Items:</label>
<input type="text" id="contactFormItems" name="contact[items]" placeholder="" />
</li>
<li>
<label>Questions:</label>
<textarea rows="10" id="contactFormComments" name="contact[body]" placeholder=""></textarea>
</li>
</ul>
<div class="clearfix"></div>
<div class="main-button-container">
<input type="submit" id="contactFormSubmit" value="Submit Enquiry" class="btn button-style" />
</div>
</div>
{% endform %}
但是,如果用户尝试提交表单但填写错误,则页面会刷新并显示错误消息并清除所有数据。从用户体验的角度来看,这并不理想,因为用户必须重新输入所有内容。
表单错误后如何保留之前填写的所有数据?请帮忙。