3
votes

Having issues trying to redirect after from submission to edit more fields Also with creating the hyperlink to the submission.

When submitting my form with the redirect i get the error below. If i submit the form with the render request to another page the form saves ok.

Request Method: POST Request URL: http://127.0.0.1:8000/resourcelib/add_pricebook Django Version: 1.8.4 Exception Type: NoReverseMatch Exception Value: Reverse for 'detail_pricebook' with arguments '()' and keyword arguments '{'p_id': 22L}' not found. 0 pattern(s) tried: []

My Model.py

class PriceBook(models.Model):
    pricebook_id = models.AutoField(primary_key=True)
    pricebook_name = models.CharField(max_length=255,verbose_name='PriceBook Name')
    discription = models.TextField(verbose_name='Discription')
    date_created = models.DateTimeField(auto_now_add=True, blank=True)
    active = models.SmallIntegerField(max_length=1, blank=True)

    def __unicode__(self):
        return self.pricebook_name

view.py

 def new_pricebook(request):
    if request.method == "POST":
        pricebook_form = PricebookForm(request.POST)
        if pricebook_form.is_valid():
                post = pricebook_form.save(commit=False)
                post.save()
                #return render(request, 'resourcelib/thanks.html',)
                return redirect('detail_pricebook', p_id=post.pricebook_id)

    else:
        pricebook_form = PricebookForm()
        return render(request, 'resourcelib/pricebook_add.html', {'pricebook_form': pricebook_form})

def detail_pricebook(request, p_id):
    pricebook_from = get_object_or_404(PriceBook, pk=p_id)
    return render(request, 'resourcelib/pricebook_detail.html', {'pricebook_from':pricebook_from})

url.py

url(r'^add_pricebook', views.new_pricebook, name='new_pricebook'),
    url(r'^list_pricebook', views.list_pricebook, name='list_pricebook'),
    url(r'^detail_pricebook/(?P<p_id>[0-9]+)/$', views.detail_pricebook, name='detail_pricebook'),

I don't believe it's in url file, because i get no errors if i browse to the entry eg. 127.0.0.1:8000/resourcelib/detail_pricebook/3/ - works ok

However i can't get my links working either, i can list my entries ok. But the link won't work from the code below when you click on the link it just comes up 127.0.0.1:8000/resourcelib/%7B%%20url%20'detail_pricebook'%20p_id.pk%7D

{% if pricebooks %}
    <ul>
    {% for pricebook in pricebooks %}
        <li><h1><a href="{% url 'detail_pricebook' p_id.pk}">{{ pricebook.pricebook_name }}</a></h1></li>
    {% endfor %}
    </ul>
{% else %}
    <p>No price books have been created.</p>
{% endif %}
{% endblock %}

Any help would be greatly appreciated

2
There is % missing. Also replace p_id with pricebook in the template: {% url 'detail_pricebook' pricebook.pk %} - ozgur

2 Answers

0
votes

% missing in the url tag. Be more careful.

0
votes

thanks @ozgur, have the the idea that i have the wrong reference for the pk. I was able to sort out the issue. I had a name name space on the primary project urls.py ... probably information i should have give, forgot I used cookiecutter for this one

url(r'^resourcelib/', include('oneworksite.resourcelib.urls', namespace='resource')),

so i was then able to get the link working with

<a href="{% url 'resource:detail_pricebook' pricebook.pk %}">{{ pricebook.pricebook_name }}</a>

Then in my redirect i changed to return redirect('resource:detail_pricebook', p_id=post.pk)