1

I think the problem is my URLconf cannot capture the board's name and process it into my views.py because i got this error

NoReverseMatch at /profile/
Reverse for 'Boat' with arguments '(u"Jimmy's Board",)' and keyword arguments '{}' not found
Error during template rendering
In template C:\o\mysite\pet\templates\profile.html, error at line 19
18     {% for b in board %}         
19     <li><a href ="{% url world:Boat b.name %}">{{ b.name }}</li>
20     {% endfor %}

This is my profile.html

 <h4>My Boards</h4>
 {% if board %} 
 <ul>  
     {% for b in board %}         
     <li><a href ="{% url world:Boat b.name %}">{{ b.name }}</li>
     {% endfor %}
 </ul>
 {% endif %}

My URLConf:

url(
    r'^board/(?P<name>\w+)/$',
    'pet.views.Boat',
    name ='Boat'
),

)

My views.py

    def Boat(request ,name=""):
    if not request.user.is_authenticated():
            return HttpResponseRedirect(reverse('world:LoginRequest'))
    if name:
        board = Board.objects.get(name=name)
        picture = Picture.objects.filter(board=board)
        return render(request,'boat.html',{'picture':picture})
    return render(request,'boat.html',{'picture':picture})
def Profile(request):
    if not request.user.is_authenticated():
        return HttpResponseRedirect(reverse('world:LoginRequest'))
    board = Board.objects.filter(user=request.user)
    person = Person.objects.get(user=request.user)
    return render(request,'profile.html',{'board':board ,'person':person})

How could I fix my URLconf to process the board name into my views.py?

My models.py

class Board(models.Model):
    user = models.ForeignKey(User)
    name = models.CharField(max_length=100)
    def __unicode__(self):
        return self.name
0

1 Answer 1

2

I think you need to use a SlugField rather than a CharField as the captured argument from your urlconf. You should't have spaces in a url.

You should be able to use the slugify function to create a slug from your name field. Maybe override the save function on your model to ensure it's an unique slug, then you can use it in your url. Something like the following would do (it's not perfect but it's quick and does the job - it just adds random digits to the name until it finds a unique slug) -

from random import randint
from django.utils.text import slugify

class Board(models.Model):
    #...
    name models.CharField(max_length=50)
    slug = models.SlugField(max_length=50 , unique=True, editable=False) 

    def save(self, *args, **kwargs):
        slug = slugify(self.name)
        unique = False
        while not unique:
            try:
                Board.objects.get(slug=slug)
                slug += str(randint(2, 9))
            except Board.DoesNotExist:
                unique = True
        self.slug = slug
        super(Board, self).save(*args, **kwargs)
Sign up to request clarification or add additional context in comments.

9 Comments

you can the browser or the app should retranslate it. Try this url "stackoverflow.com/questions/15355825/django regex error" But you are correct in that he should use a slugfield as its so much better.
If I use SlugField , would I be able to use space in URL>
@AidanEwen because it looks sweet as fudge. Thats why. Also, its more readable if you have something that looks like this "my-monkey-stole-my-underwear-1251928-and-then-1239824" than with spaces.
@donkeyboy72 you don't want spaces in the url. That's bad practice. Go with the slugfield.
I see so SlugField replace the space with -?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.