I don't know why I get this error while trying to reach the admin change_list page of the mymodel model:
NoReverseMatch at /admin/myapp/mymodel/
Reverse for 'myapp_mymodel_change' with arguments '(u'',)' and keyword arguments '{}' not found.
Error during template rendering
In template /usr/local/lib/python2.7/dist-packages/django/contrib/admin/templates/admin/change_list.html, error at line 91
84 <form id="changelist-form" action="" method="post"{% if cl.formset.is_multipart %} enctype="multipart/form-data"{% endif %}>{% csrf_token %}
85 {% if cl.formset %}
86 <div>{{ cl.formset.management_form }}</div>
87 {% endif %}
88
89 {% block result_list %}
90 {% if action_form and actions_on_top and cl.full_result_count %}{% admin_actions %}{% endif %}
91 {% result_list cl %}
92 {% if action_form and actions_on_bottom and cl.full_result_count %}{% admin_actions %}{% endif %}
93 {% endblock %}
94 {% block pagination %}{% pagination cl %}{% endblock %}
95 </form>
The error does not occur on my local runserver(Python 2.7.5, Django 1.5.1), only when deployed on my remote server (Python 2.7.2+, Django 1.5.1). Interestingly enough, only one specific model is affected by this -- I can reach the change_list pages of all the other models without any problems whatsoever. The rest of the admin area works fine as well.
The answers to similar questions regarding the NoReverseMatch error didn't help much because it happens in the admin, not my own code. Does anyone know where to start looking?
EDIT:
I had some customised list_display fields which I now commented out for testing. They seemed to be responsible for the NoReverseMatch error. Now I got another error instead:
AttributeError at /admin/myapp/mymodel/
'NoneType' object has no attribute '_meta'
I then stripped away everything that isn't necessary. This is now my complete admin.py:
from django.contrib import admin
from myproject.myapp.models import *
class MymodelAdmin(admin.ModelAdmin):
list_display = ['email', 'user', 'is_active', 'first_used']
date_hierarchy = 'first_used'
ordering = ['-first_used']
list_filter = ['is_active', 'first_used', 'user']
admin.site.register(Mymodel, MymodelAdmin)
And on my local machine it still works perfectly.
There are some variables which are not getting passed during rendering the template ..
Take your lead by checking the required local variables to be passed as to fulfill the url pattern in url tag ..
I encountered same problem when my foreign key is null while using this script not originally mine on my admin change_list.html.
What it does is to display the column for foreign_key of your model.
from django.forms import MediaDefiningClass
class ModelAdminWithForeignKeyLinksMetaclass(MediaDefiningClass):
def __getattr__(cls, name):
def foreign_key_link(instance, field):
# Please note in Django 1.6
# Model._meta.module_name was renamed to model_name
target = getattr(instance, field)
return u'%s' % (
target._meta.app_label, target._meta.model_name, target.id, unicode(target))
if name[:8] == 'link_to_':
method = partial(foreign_key_link, field=name[8:])
method.__name__ = name[8:]
method.allow_tags = True
setattr(cls, name, method)
return getattr(cls, name)
raise AttributeError
I added a line if target: in filter out those null values.
if target:
return u'%s' % (
target._meta.app_label, target._meta.model_name, target.id, unicode(target))
Script usage:
where carrier,sitename are a foreign key field.
class LatestRsl_v2Admin(admin.ModelAdmin):
__metaclass__ = classmaker(right_metas=(ModelAdminWithForeignKeyLinksMetaclass,))
list_display = ['issued', 'link_to_carrier', ]
search_fields = ['=rslno','=issued','link_to_carrier', 'link_to_sitename']
Related
I'm diving into the static site generators and trying to build a blog out using the basic eleventy blog:
https://github.com/11ty/eleventy-base-blog
So far, everything is good, but I'm wondering how to go about to get it to show the latest post on the home/index page.
Right out of the box, it will show the 3 latest links to the posts using nunjucks:
{% set postslist = collections.posts | head(-3) %}
{% set postslistCounter = collections.posts | length %}
{% include "postslist.njk" %}
But what I'm wondering how to get the latest markdown post from the posts directory and see if it can pull that to render.
I am thinking that there's got to be a way to check the date within nunjucks like this (I know this is wrong, but trying to get an idea):
{% if post.date = currdate %}
{% include "posts.njk" %}
{% endif %}
Anyways, I know it's possible, but I'm still trying to learn and looking to get pointed in the right direction.
I think this could work for you:
{% set postslist = collections.posts | head(-3) %}
<h1>Latest Post</h1>
{{ postslist[0].templateContent | safe }}
I basically just use the templateContent variable of the first post. I moved that set command higher in the template so I could use another H3. Here's my entire file:
---
layout: layouts/home.njk
eleventyNavigation:
key: Home
order: 1
---
{% set postslist = collections.posts | head(-3) %}
<h1>Latest Post</h1>
{{ postslist[0].templateContent | safe }}
<h1>Latest 3 Posts</h1>
{% set postslistCounter = collections.posts | length %}
{% include "postslist.njk" %}
<p>More posts can be found in the archive.</p>
I would like to set up a hash variable using strings from an array dynamically (instead of writing 1000 lines of code).
As well I would like to use a dynamically created string to access a hash by using it as the key - for a built-in (what I assume is a hash) object - settings. Settings allow you to access data in settings_schema.json, eg: settings.my_custom_setting
According to this documentation: https://github.com/Shopify/liquid/wiki/Liquid-for-Designers
"For hashes, the key must be a literal quoted string or an expression that resolves to a string."
so I have tried {% assign filter[thisFilter] = false %} but get an error: ParseError: illegal token
First issue / accessing hash key with a variable:
{% comment %} All possible filters {% endcomment %}
{% assign allFilters = "color,size,collection,style,height,function,artist" %}
{% assign allFiltersArray = allFilters | split ',' %}
{% comment %} hash of filters each set to false {% endcomment %}
{% for thisFilter in allFiltersArray %}
{% assign filter[thisFilter] = false %}
{% endfor %}
Second issue, accessing settings object with a dynamically generated key:
{% comment %} set to true whichever filters are configured in settings for this collection {% endcomment %}
{% for thisCollection in allCollectionsArray %}
{% if thisCollection == currentCollection %}
{% for thisFilter in allFiltersArray %}
{% assign itemToCheck = "filter_" | append: thisCollection | append: "_" | append: thisFilter %}
{% if settings[itemToCheck] %}
{% assign filter[thisFilter] = true %}
{% endif %}
{% endfor %}
{% endif %}
{% endfor %}
In the first issue, I expect the result to be a hash such as:
filter['color'] = false (or filter.color = false)?
filter['size'] = false
In the second issue, I'm expecting something like:
{% if settings.filter_shirts_color %}
What you are trying to do is not possible. If you read further on your provided link Liquid for Designers, it is mentioned
Note that there is no way to write a literal array or hash as an
expression; arrays and hashes must be passed into the template, or
constructed obliquely with a tag or output statement.
Moreover, even if you have such hash, you cannot assign it new value. For example,
{% assign settings['some-setting-id'] = false %}
This will not work. Same is the case with array created using split filter. You cannot assign new values on any index.
For the second issue, this should work, the error in your case most probably is because of invalid generated string or there is no setting with that id. This should work fine and display value for that setting.
{%assign string_key = 'setting-key'%}
{{settings[string_key]}}
But this will not work because
{%assign string_key = 'setting-key'%}
{{settings.string_key}}
my_hash.key — Hashes also allow a shorter "dot" notation, where the
name of the variable is followed by a period and the name of a key.
This only works with keys that don't contain spaces, and (unlike the
square bracket notation) does not allow the use of a key name stored
in a variable.
I have this code inside my html:
{% for macchine in range(20) %}
{% set macchina_usata = 'M'+ macchine|string %}
{{ data['macchina_usata'] }}
{% if data['macchina_usata'] is defined %}
do something..
{% endif %}
{% endfor %}
before it was without the for cycle, I just had to check if a variable is defined and I got the result, but now I want to put it in a Cycle for because I have to check 20 or more variables.
The variables that I got from the previous html are like M1, M2, M3, ... M20 then I thought that was a good idea to create a varible macchina_usata composed by M+ the int macchine converted in a string, but when I try to print it nothing happen, so, i guess that I'm using the Alias in a wrong way
You're using the literal string 'macchina_usata' as an index for data. You should instead use variable macchina_usata, without the quotes:
{{ data[macchina_usata] }}
{% if data[macchina_usata] is defined %}
In my shopify store, I am using SuperFields in order to customize my site, though my question isn't about the app. On one of my pages, I need the value for the following:
variant.metafields.sf_{{ collection.title | downcase }}[meta_tag_key]
The value should be 0 or 1. If I evaluate the statement directly, such as:
{if variant.metafields.sf_{{ collection.title | downcase }}[meta_tag_key] =1%}
It throws an error when I render the page: Unexpected character '{'
I've also tried the following:
{% capture defaultImage %}variant.metafields.sf_{{ collection.title | downcase }}[meta_tag_key]{% endcapture %}
{% assign test = defaultImage %}
But 'test' is considered nil and doesn't return any value. I have tried to search for answers here and on the shopify forum, but, as my clumsy post title suggests, I'm having a hard time concisely searching for a solution to this problem. Any help is greatly appreciated.
You can try :
{% assign metafield-key = collection.title | downcase | prepend: "sf_" %}
{% assign key = variant.metafields[metafield-key][meta_tag_key] %}
{% if key == 1 %}
Do the twist !
{% endif %}
You are missing a % sign in your code. Hence the error message. Your if statement started with {% and not just {
If you working in liquid then you have to use {% %} for defining any variable & also for condition in shopify. You can't use { this.
I made a custom link tag in Liquid and I am trying to be able to pass liquid variables into the call for that tag like so
{{ assign id = 'something' }} // this value is actual dynamic while looping through data
{% link_to article: id, text: 'Click Me!' %} // my custom tag
However this results in the article parameter being passed in as 'id' instead of 'something' as per the assign statement above it.
Does anyone know how to pass variables into tag calls?
I've recently solved this very simply with Jekyll 0.11.2 and Liquid 2.3.0 by passing the name of the variable as the tag parameter.
{% assign v = 'art' %}
{% link_to_article v %}
You can also pass the name of the control var while in a loop, like article above.
In Liquid::Tag.initialize, #markup is the second parameter, the string following the tag name. The assigned variables are available in the top level of the context.
def render(context)
"/#{context[#markup.strip]}/"
end
This obviously only allows one param to be passed. A more complex solution would parse params like x: 2, y: 3.
This solved the case for me context[#markup.strip].
My problem was that i wanted to be able to pass a variable to my custom Liquid tag like this: {% get_menu main_menu navigation.html settings.theme.id %}
In order to do this i first split the variable string into different varaibles on every space character.
class GetMenu < Liquid::Tag
include ApplicationHelper
def initialize(tag_name, variables, tokens)
#variables = variables.split(" ")
#menu_object = #variables[0]
#file_name = #variables[1]
#theme_id = #variables[2]
super
end
def render(context)
# This is where i use context[#theme_id.strip] to get the variable of "settings.theme.id"
content = CodeFile.find_by(hierarchy: 'snippet', name: #file_name.to_s, theme_id: context[#theme_id.strip])
#menu ||= Menu.find_by_slug(#menu_object)
context.merge('menu' => #menu)
Liquid::Template.parse(content.code).render(context)
end
end
Liquid::Template.register_tag('get_menu', GetMenu)
*This is just a more rich example that the answer above by Jonathan Julian
Doesn't look like this is possible, my solution was to just pass the variable name in to the tag and grab it out of the context the tag is being rendered in. Like so:
{% for article in category.articles %}
{% link_to variable: article, text: title %}
{% endfor %}
in my tag code (condensed):
def render(context)
uri = "article/#{context[#options[:variable]]['id']}"
"<a href='#{uri}'>#{build_link_text context}</a>"
end
It would be great to have a tag that can be called with literals and variables like
{% assign v = 'art' %}
{% link_to_article v %}
or
{% link_to_article 'art' %}
or
{% link_to_article "art" %}
and also of course
{% link_to_article include.article %}
In order to so I propose a helper function
def get_value(context, expression)
if (expression[0]=='"' and expression[-1]=='"') or (expression[0]=="'" and expression[-1]=="'")
# it is a literal
return expression[1..-2]
else
# it is a variable
lookup_path = expression.split('.')
result = context
puts lookup_path
lookup_path.each do |variable|
result = result[variable] if result
end
return result
end
end
And in the render just call the helper function to get the value of the literal or variable.
def render(context)
v = get_value(context, #markup.strip)
end
FYI, the initialiser would look like this:
def initialize(tag_name, markup, tokens)
#markup = markup
super
end
This does not strictly answer the question, but it may help others who are new to Liquid (like myself) and try something like this. Instead of implementing a custom tag, consider implementing a custom filter instead. Variables are resolved before they are passed into filters.
Ruby code:
module MyFilters
def link_to_article(input, text)
"<a href='https://example.org/article/#{input}'>#{text}</a>"
end
end
Liquid::Template.register_filter(MyFilters)
Liquid template:
{% assign id = 'something' %}
{{ id | link_to_article: 'Click Me!' }}
Output:
<a href='https://example.org/article/something'>Click Me!</a>
You can also use variables as parameters. So the following would have the same output:
{% assign id = 'something' %}
{% assign text = 'Click Me!' %}
{{ id | link_to_article: text }}
And filters can have zero or more (comma-separated) parameters:
{{ 'input' | filter_with_zero_parameters }}
{{ 'input' | filter_with_two_parameters: 'parameter 1', 'parameter 2' }}