OK here are what I think are the relevant parts:
settings.py
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR,'static'), # if your static files
folder is named "staticfiles"
)
# Redirect to home URL after login (Default redirects to /accounts/profile/)
LOGIN_REDIRECT_URL = '/'
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
urls.py
urlpatterns = [
path('', views.index, name='index'),
path('cookbooks/', views.CookBookListView.as_view(), name='cookbooks'),
path('cookbook/<uuid:pk>', views.CookBookDetailView.as_view(),
name='cookbook'),
path('recipe/', views.RecipeListView.as_view(), name='recipes'),
path('recipe/<uuid:pk>', views.RecipeDetailView.as_view(), name='recipe'),
path('family/', views.FamilyDetailView.as_view(), name='family'),
path('recipe/add/', views.RecipeAdd, name='addrecipe'),
#Add Django site authentication urls (for login, logout, password
management)
]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
models.py
class Recipe(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4,
help_text='Unique ID for this particular recipe across whole library')
cookbook = models.ForeignKey(CookBook,on_delete=models.SET_NULL,
null=True, related_name='recipies')
title = models.TextField(max_length=1000, help_text='Enter a brief
description of the Recipe')
author = models.TextField(max_length=1000, help_text='Enter the
creator of the Recipie', blank=True)
text = models.TextField(max_length=10000, help_text='Enter the recipe')
notes = models.TextField(max_length=10000, help_text='Enter notes
on the recipe',blank=True)
pic = models.ImageField(upload_to='media/',blank=True, null=True)
views.py
def RecipeAdd(request):
template_name = 'recipe_add.html'
r = Recipe(cookbook=Family.objects.get(members=request.user).FamilyCookbook)
# If this is a POST request then process the Form data
if request.method == 'POST':
# Create a form instance and populate it with data from the
request (binding):
add_recipe_form = AddRecipeForm(request.POST, request.FILES)
# Check if the form is valid:
if add_recipe_form.is_valid():
# process the data in form.cleaned_data as required (here
we just write it to the model due_back field)
# book_instance.due_back =
book_renewal_form.cleaned_data['renewal_date']
r.author = add_recipe_form.cleaned_data['author']
r.notes = add_recipe_form.cleaned_data['notes']
r.text = add_recipe_form.cleaned_data['text']
r.title = add_recipe_form.cleaned_data['title']
# r.pic=add_recipe_form.cleaned_data['pic'] # file not
being returned here
# r.pic = add_recipe_form.data['pic'] # file not being
returned here
r.pic = request.FILES['pic'] #file not here
add_recipe_form.save() # save the recipe
# redirect to a new URL:
return HttpResponseRedirect(reverse('recipes'))
# If this is a GET (or any other method) create the default form.
else:
# proposed_renewal_date = datetime.date.today() +
datetime.timedelta(weeks=3)
add_recipe_form = AddRecipeForm()
context = {
'form': add_recipe_form,
}
return render(request, 'recipe_add.html', context)
forms.py
class AddRecipeForm( ModelForm ):
pic = ImageField(widget= FileInput) # was widget=PictureWidget
class Meta:
model = Recipe
fields = ('title', 'author', 'text', 'notes', 'pic')
widgets = {
'title': Textarea( attrs={'cols': 40, 'rows': 1} ),
'author': Textarea( attrs={'cols': 40, 'rows': 1} ),
'text': Textarea( attrs={'cols': 40, 'rows': 1} ),
'notes': Textarea( attrs={'cols': 40, 'rows': 1} ),
# 'pic': Textarea(attrs={'cols': 40, 'rows': 1}),
recipe_list.html
<h1>Recipe List</h1>
{% if recipe_list %}
<ul>
{% for recipe in recipe_list %}
<li>
<!-- <a href="{{ recipe.get_absolute_url }}">{{ recipe.title
}}</a> {{recipe.author}} -->
<p>{{ recipe.title }}</p> {{recipe.author}}
<p> {{recipe.text}} <p>
<p>{{ recipe.notes }}</p>
{% load static %}
<div class="recipe_image">
{% if recipe.pic %}
<a href="{{ recipe.pic.url }}">{{ recipe.title }}</a>
<img src="{{ recipe.pic.url}}" alt="{{ recipe.title }}" />
{% endif %}
</div>
<p> {{ recipe.pic }}</p>
</li>
{% endfor %}
Post by Joel MathewIt's anybody's guess unless you post your code.
Sincerely yours,
Joel G Mathew
Post by Jeff WilliamsHi All,
I'm new to django, so sorry if this is a newbie issue.
I've managed to upload an ImageField....but when I try to display it in
my template using object.pic.url...the URL I'm getting back is missing my
project name from the URL and the pic is not loading.
Specifically I'm getting
http://127.0.0.1:8000/media/media/56288852934__75FACD12-1711-4655-96D5-716198FE10B7.JPG back.....and
I need
http://127.0.0.1:8000/*oldgrub*
/media/media/56288852934__75FACD12-1711-4655-96D5-716198FE10B7.JPG
back to have it work.
What am I missing? I've poured over my configs.
Jeff
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/CAMgbqCe3UzvrWFg4fiRSPXp6HtLJoPJmGs8oPSZjAd%3Dxx4ygNQ%40mail.gmail.com
<https://groups.google.com/d/msgid/django-users/CAMgbqCe3UzvrWFg4fiRSPXp6HtLJoPJmGs8oPSZjAd%3Dxx4ygNQ%40mail.gmail.com?utm_medium=email&utm_source=footer>
.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/CAA%3Diw_-Siy3yQgn8O9Kj0NC6URxwhebR%3DqO7BvUwzyL9mi9NdQ%40mail.gmail.com
<https://groups.google.com/d/msgid/django-users/CAA%3Diw_-Siy3yQgn8O9Kj0NC6URxwhebR%3DqO7BvUwzyL9mi9NdQ%40mail.gmail.com?utm_medium=email&utm_source=footer>
.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+***@googlegroups.com.
To post to this group, send email to django-***@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAMgbqCfqXvKzwPg%3D2nVqZPos9ZRRyANTWWb2%2BX9eufFoJQbMWw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.