I want my page content to vary depending on which page the user came from.
How is it possible to define the previous page to use it in a template?
I mean it should look like
{% if (previous page)=="/some/page/"%}
do something
{% endif %}
What do I have to write instead of (previous page)?
I wonder if I can use request.META['HTTP_REFERER'] somehow, because all my attempts called errors.
Django's template language is NOT Python and doesn't support the Python's subscript syntax. You have to use request.META.HTTP_REFERER as documented here : https://docs.djangoproject.com/en/dev/topics/templates/#variables
Also the http referer meta is not necessarily reliable so you might be safest using sessions to track your users navigation.
Related
So I am trying to build something for my store but a few things are somewhat unclear.
If I need to save some user settings do I need my own backend just
for that app specifically? For simplicity sake, I want build an app
to save and display a custom message in cart-template.liquid.To
achieve that, I think my app should make a request to my backend (let
say, on heroku) and save it in some db that app is using?
How do I retrieve that data in cart-template.liquid? I guess I
build a snippet that calls a public endpoint of my backend that
returns that saved message using fetch() or maybe axios.get and
embed it using {% render 'fetch-custom-message-snippet' %} ?
Say I ask for user input, ie. "Engraved message" and the form is in cart-template.liquid,
of course. The following snippet is used:
<p class="line-item-property__field">
<label for="engraved-message">Engraved message</label>
<input id="engraved-message" type="text" name="properties[Engraved message]">
</p>
How do I make sure that bit of information is captured and passed to me? I guess I want to see it somewhere in the order details.
If I need to save some user settings do I need my own backend just for that app specifically? For simplicity sake, I want build an app to save and display a custom message in cart-template.liquid.To achieve that, I think my app should make a request to my backend (let say, on heroku) and save it in some db that app is using?
Yes, you need your own backend. Your application alone is responsible for storing its own information (there are some exceptions like a special order field which I show you below) - that typically infers a database that back ups your service and holds your data. Please check out this thread as you can find lots of valuable information there.
Regarding cart-template.liquid I'd suggest taking a look at the official "Shopify Developers" documentation. All information you're allowed to display and request are neatly explained and ordered there.
How do I retrieve that data in cart-template.liquid? I guess I build a snippet that calls a public endpoint of my backend that returns that saved message using fetch() or maybe axios.get and embed it using {% render 'fetch-custom-message-snippet' %} ?
Once again there are good guides out there. I suggest taking a look at this blog post which goes into in-depth on this topic. Shopify's documentation about the Liquid template language is also highly advised to be read.
How do you retrieve that data? According to this specific example any input will be supplied to your order page in the Shopify admin. For example:
<label for="CartNote">Special instructions</label>
<textarea name="note" id="CartNote">{{ cart.note }}</textarea>
*taken from https://shopify.github.io/liquid-code-examples/example/cart-notes*; shows a Special instruction label and textarea for users to submit details about the oder - you will get this data on, as mentioned, the order page in the Shopify admin.
Say I ask for user input, ie. "Engraved message" and the form is in
cart-template.liquid, of course. The following snippet is used:
[...] How do I make sure that bit of information is captured and passed to me? I guess I want to see it somewhere in the order details.
see above
//EDIT:
To prevent any confusion: It seems like you want to develop a custom app just for personal usage and not to publish it in the Shopify App Store - in this case you most often than not don't need an external database; e.g. the example you provided with a simple order request which is easily doable through Shopify's examples.
For your specific case this code snippet (I modified your original example to fit the case - it's not a full cart-template.liquid obviously; in this case the file is called cart.liquid):
<label for="engraved-message">Engraved message</label>
<textarea name="message" id="engraved-message">{{ cart.note }}</textarea>
//EDIT 2:
The link - shared by another user in this thread, namely #Simas Butavičius - is actually kind of useful if you have problems with the customization process in general, i.e. if you want to revise some basic concepts or want to check how to implement the code snippet from above in the whole structure of your website I'd advise to skim through this site.
Needless to say, there are hundreds of good tutorials, questions regarding the same "issue" or other resources in general.
I suggest for further reading purposes to check out some of these links and guides (some may be mentioned above):
https://shopify.github.io/liquid-code-examples/example/cart-notes
https://shopify.github.io/liquid-code-examples/example/checkout-form (! very good in-depth example)
https://www.christhefreelancer.com/shopify-liquid-guide/
https://shopify.dev/docs/themes/theme-templates/cart-liquid
https://shopify.dev/docs/themes/liquid/reference
https://www.shopify.com/partners/shopify-cheat-sheet (! helpful cheatsheet)
https://community.shopify.com/c/Shopify-Design/Cart-Use-cart-attributes-to-collect-more-information/td-p/613718
https://community.shopify.com/c/Shopify-APIs-SDKs/Add-custom-input-fields-to-cart/td-p/154710
https://community.shopify.com/c/Shopify-Design/Product-pages-Get-customization-information-for-products/td-p/616503
Here is the tutorial specifically for creating custom shopify input field for getting engraving information: https://community.shopify.com/c/Shopify-Design/Product-pages-Get-customization-information-for-products/td-p/616503
I need to get blog articles from my storefront in JSON format, as the limited amount of articles you can fetch on any single page request is 50 . A bonus would be to be able filter articles based on tag.
Getting JSON from the Shopify storefront is possible through the AJAX API, but is only limited to products and cart. I know you can create an alternate template for everything else, but how do you do it for the blog?
I've tried this but it won't work:
https://domain.myshopify.com/admin/blogs/blog_id/articles.json
You talk about Storefront, but you are giving Admin API URL's. You can't request the admin from the storefront without using GraphQL or the Rest API!
Liquid way
You are limited by 50 articles if you don't overwrite the paginate on the storefront.
But if you overwrite it you can get as much as you like. ( have in mind that the larger the article pool the longer the DOM will load )
Example:
{% paginate blog.articles by 9999 %}
{% for article in blog.articles %}
{% endfor %}
{% endpaginate %}
You can create a separate blog template and request it with AJAX and add the tag to the end as well.
So if you create a blog template called blog.ajax.liquid your request will be something like so: /blogs/news/tagged/featured?view=ajax and it will return the html for the new template filtered by the tag featured.
GraphQL way
The other way is to use the storefront GraphQL in order to get the articles.
You will need to create a private app and allow Read content like articles, blogs, and comments in order to use this.
Example query:
{
blogByHandle(handle:"news"){
articles(first: 50, query:"tag:featured") {
edges {
node {
title
}
}
}
}
}
Where this will return 50 article titles which have a tag called featured, you can add more fields that you like the query to return of course.
REST API
The other way is to use the REST API.
You still need to create a private app, but you must allow only the Blog & Article reading rights, no writing permissions. In addition all other rights should be disabled, so that you don't allow other to modify your store data.
The AJAX url will be something like this: https://API_KEY:API_PASSWORD#YOUR_STORE.myshopify.com/admin/api/2020-01/blogs/BLOG_ID/articles.json?tag=featured
I don't recommend this approach, but it will still work.
From there you choose what way you like to go.
Have you tried below end point ?
https://domain.myshopify.com/admin/blogs/blog_id/articles.json?tag={tag_name}
And In the 1 call you can get upto 250 objects you will need to pass limit as shown below
https://domain.myshopify.com/admin/blogs/blog_id/articles.json?limit=250
Is there any way to change the actual structure of a url in Shopify, rather than just the handle? For instance if I add a product it will be available at the following urls:
/products/some-product
/collections/collection-name/products/some-product
Is there any way I could change this to /collection-name/some-product, i.e. remove unnecessary words from the url?
I also realise you can add redirects, but this isn't what I want.
When thinking on the product page you should never think of playing or using the url which has 'collections'. If you take a deep look on the source code of a product you'll realize they all have a rel canonical tag pointing to the
../products/some-product
even if the product is displayed within the url
../collections/collection-name/products/some-product
If the collections url doesn't have that canonical tag, use it, otherwise crawlers/robots would consider it duplicate content because 2 different urls would show the same content.
Then if you're ok with the first part, you'll only have
../products/some-product
In such case, you will never be able to change the
../products/
part. But this is good as it helps Shopify store owners maintain a really well structured organization of products.
If you still for some reason need to play hard with urls, you can deep a bit into Application Proxies.
I'm displaying a page with around 60 avatars using django-avatar and a query is made for each one of them.
I cannot use User.objects.select_related('avatar') because there is no link between my user and his avatar. So how do I optimize this?
EDIT:
Avatars are retrieved in the template using {% avatar user %} (this is a template tag specific to github.com/jezdez/django-avatar, which seems to be the most used application for handling user avatars. I'm asking this question because some people certainly already had to face the issue of displaying many avatars using this application and I would like to know their solution).
Using the cache (like memcached) is the answer.
So I have a list present in the template context and I want to apply random and then access an attribute of the resulting object.
So far, what I've read in the template section of the django documentation, filters always have to come after the variable so I understand (even though I tried it anyway) that
{{ my_list|random.attribute }}
won't work.
Right now I'm using a custom filter to access attribute, so I can chain it after any other filter, but what would be the best solution for this?
Use with to give the result another name temporarily.